• Gobbel2000@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    So true. Every time I have to look up how to write a bash for loop. Where does the semicolon go? Where is the newline? Is it terminated with done? Or with end? The worst part with bash is that when you do it wrong, most of the time there is no error but something completely wrong happens.

    • ClemaX@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months ago

      It all makes sense when you think about the way it will be parsed. I prefer to use newlines instead of semicolons to show the blocks more clearly.

      for file in *.txt
      do
          cat "$file"
      done
      

      The do and done serve as the loop block delimiters. Such as { and } in many other languages. The shell parser couldn’t know where stuff starts/ends.

      Edit: I agree that the then/fi, do/done case/esac are very inconsistent.

      Also to fail early and raise errors on uninitialized variables, I recommend to add this to the beginning of your bash scripts:

      set -euo pipefail
      

      Or only this for regular sh scripts:

      set -eu
      

      -e: Exit on error

      -u: Error on access to undefined variable

      -o pipefail: Abort pipeline early if any part of it fails.

      There is also -x that can be very useful for debugging as it shows a trace of every command and result as it is executed.

    • qjkxbmwvz@startrek.website
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months ago

      I can only remember this because I initially didn’t learn about xargs — so any time I need to loop over something I tend to use for var in $(cmd) instead of cmd | xargs. It’s more verbose but somewhat more flexible IMHO.

      So I run loops a lot on the command line, not just in shell scripts.

  • Pixelbeard@lemmy.ca
    link
    fedilink
    Français
    arrow-up
    0
    ·
    2 months ago

    Je comprend tellement! Je répond en français pour ma première réponse sur Lemmy juste pour voir comment ça va être géré!

    • Pixelbeard@lemmy.ca
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      I so understand! Answering I. French for my first Lemmy reply just to see how it’s handled.

      Realizing now that language selection is mainly for people filtering. It be cool if it auto translated for people that need it.

      • Pixelbeard@lemmy.ca
        link
        fedilink
        arrow-up
        0
        ·
        1 month ago

        En un mundo ideal. Todo se traduciría automáticamente del idioma original al idioma del lector y viceversa

  • brokenlcd@feddit.it
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    Knowing that there is still a bash script i wrote around 5 years ago still running the entirety of my high scool lab makes me sorry for the poor bastard that will need to fix those hieroglyphs as soon as some package breaks the script. I hate that i used bash, but it was the easiest option at the time on that desolate server.

    • formulaBonk@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      Bash scripts survive because often times they are the easiest option on an abandoned server

  • JTskulk@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 months ago

    Bash was the first language I learned, got pretty decent at it. Now what happens is I think of a tiny script I need to write, I start writing it in Bash, I have to do string manipulation, I say fuck this shit and rewrite in Python lol

  • jkercher@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 months ago

    Meh. I had a bash job for 6 years. I couldn’t forget it if I wanted to. I imagine most people don’t use it enough for it to stick. You get good enough at it, and there’s no need to reach for python.

  • LeninOnAPrayer@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    2 months ago

    The sad thing is that even chatgpt can’t program in bash. I just want a simple script and every single time it just doesn’t work. I always just end up saying “write this in python instead”.

    • Hawk@lemmynsfw.com
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      Python’s usually the better choice anyway tbf. I know piping isn’t as good, but there are so many footguns!

      Nushell and Fish can be really convenient too.

      I used to adhere to sh for an OpenBSD machine but I switched to python, Rust and Go for, even simple things.

      • sunstoned@lemmus.org
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 months ago

        Python is just as portable these days (on modern hardware, caveats, caveats).

        Honestly so intuitive that I start there too unless I have a need for speed or distinct memory control. There’s no job too small for a python script.

    • CrazyLikeGollum@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      Or scripts for basically any other variant of the Bourne shell. They are, for the most part, very cross compatible.

      • Tinidril@midwest.social
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 months ago

        That’s the only reason I’ve ever done much of anything in shell script. As a network administrator I’ve worked many network appliances running on some flavor of Unix and the one language I can count on to be always available is bash. It has been well worth knowing for just that reason.

      • BeigeAgenda@lemmy.ca
        link
        fedilink
        arrow-up
        0
        ·
        2 months ago

        I wrote a script to do backups on a ESXi it uses Busybox’s ASH, one thing I learned after spending hours debugging my scripts was that ASH does not support arrays so you have to do everything with temporary files.

        • YouAreLiterallyAnNPC@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          2 months ago

          There actually is an array in any POSIX shell. You get one array per file/function. It just feels bad to use it. You can abuse ‘set – 1 2 3 4’ to act as a proper array. You can then use ‘for’ without ‘in’ to iterate over it.

          for i; do echo $i; done.

          Use shift <number> to pop items off.

          If I really have to use something more complex, I’ll reach for mkfifo instead so I can guarantee the data can only be consumed once without manipulating entries.

  • perishthethought@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 months ago

    I don’t normally say this, but the AI tools I’ve used to help me write bash were pretty much spot on.

    • SpaceNoodle@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months ago

      Yeah, an LLM can quickly parrot some basic boilerplate that’s showed up in its training data a hundred times.

    • marduk@lemmy.sdf.org
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      Yes, with respect to the grey bearded uncles and aunties; as someone who never “learned” bash, in 2025 I’m letting a LLM do the bashing for me.

    • henfredemars@infosec.pub
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      For building a quick template that I can tweak to my needs, it works really well. I just don’t find it to be an intuitive scripting language.

    • cm0002@lemmy.worldOP
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      For a defacto windows admin my Powershell skills are…embarrassing lol but I’m getting there!

  • Victor@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    Ever since I switched to Fish Shell, I’ve had no issues remembering anything. Ported my entire catalogue of custom scripts over to fish and everything became much cleaner. More legible, and less code to accomplish the same things. Easier argument parsing, control structures, everything. Much less error prone IMO.

    Highly recommend it. It’s obviously not POSIX or anything, but I find that the cost of installing fish on every machine I own is lower than maintaining POSIX-compliant scripts.

    Enjoy your scripting!

    • LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      I wish I could but since I use bash at work (often on embedded systems so no custom scripts or anything that isn’t source code) I just don’t want to go back and forth between the two.

    • UndercoverUlrikHD@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      If you’re going to write scripts that requires installing software, might as well use something like python though? Most Linux distros ship also ship with python installed

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        2 months ago

        A shell script can be much more agile, potent, and concise, depending on the use case.

        E.g. if you want to make a facade (wrapper) around a program, that’s much cleaner in $SHELL. All you’re doing is checking which keyword/command the user wanted, and then executing the commands associated with what you want to achieve, like maybe displaying a notification and updating a global environment variable or something.

        Executing a bunch of commands and chaining their output together in python is surely much more cumbersome than just typing them out next to each other separated by a pipe character. It’s higher-level. 👍

        If it’s just text in text out though, sure, mostly equivalent, but for me this is rarely the use case for a script.

        • UndercoverUlrikHD@programming.dev
          link
          fedilink
          arrow-up
          0
          ·
          2 months ago

          I’m not anti bash or fish, I’ve written in both just this week, but if we’re talking about readability/syntax as this post is about, and you want an alternative to bash, I’d say python is a more natural alternative. Fish syntax is still fairly ugly compared to most programming languages in my opinion.

          Different strokes for different folks I suppose.

    • alt_xa_23@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      I switched to fish a while back, but haven’t learned how to script in it yet. Sounds like I should learn

    • raldone01@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      I love fish but sadly it has no proper equivalent of set -e as far as I know.

      || return; in every line is not a solution.

        • activ8r@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          0
          ·
          2 months ago

          I know that LLMs are probably very helpful for people who are just getting started, but you will never understand it if you can’t grasp the fundamentals. Don’t let “AI” make you lazy. If you do use LLMs make sure you understand the output it’s giving you enough to replicate it yourself.

          This may not be applicable to you specifically, but I think this is nice info to have here for others.

    • 9point6@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      You get used to it, I don’t even see the code—I just see: group… pattern… read-ahead…

    • kameecoding@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      Most of regex is pretty basic and easy to learn, it’s the look ahead and look behind that are the killers imo

    • Kissaki@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 months ago

      You always forget regex syntax?

      I’ve always found it simple to understand and remember. Even over many years and decades, I’ve never had issues reading or writing simple regex syntax (excluding the flags and shorthands) even after long regex breaks.

      • Akito@lemmy.zip
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 months ago

        It’s not about the syntax itself, it’s about which syntax to use. There are different ones and remembering which one is for which language is tough.

          • ewenak@jlai.lu
            link
            fedilink
            arrow-up
            0
            ·
            2 months ago

            There is the “very magic” mode for vim regexes. It’s not the exact PCRE syntax, but it’s pretty close. You only need to add \v before the expression to use it. There is no permanent mode / option though. (I think you can remap the commands, like / to /\v)

        • Lehmanator@programming.dev
          link
          fedilink
          English
          arrow-up
          0
          ·
          2 months ago

          This is exactly it. Regex is super simple. The difficulty is maintaining a mental mapping between language/util <-> regex engine <-> engine syntax & character class names. It gets worse when utils also conditionally enable extended syntaxes with flags or options.

          The hardest part is remembering whether you need to use \w or [:alnum:].

          Way too few utils actually mention which syntax they use too. Most just say something accepts a “regular expression”, which is totally ambiguous.

  • 74 183.84@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 months ago

    And I thought I was the only one… for smaller bash scripts chatGPT/Deepseek does a good enough job at it. Though I still haven’t tried VScode’s copilot on bash scripts. I have only tried it wirh C code and it kiiiinda did an ass job at helping…

    • cm0002@lemmy.worldOP
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      AI does decently enough on scripting languages if you spell it out enough for it lol, but IMO it tends to not do so well when it comes to compiled languages

      I’ve tried Python with VScode Copilot (Claude) and it did pretty good