• @Gobbel2000@programming.dev
    link
    fedilink
    121 month 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
      13
      edit-2
      1 month 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 initialized 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
      2
      edit-2
      1 month 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.

  • 74 183.84
    link
    fedilink
    English
    111 month 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
      61 month 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

        • @cm0002@lemmy.worldOP
          link
          fedilink
          51 month ago

          I was chalking it up to some scripting languages just tending to be more popular (like python) and thus having more training data for them to draw from

          But that’s a good point too lol

      • 74 183.84
        link
        fedilink
        English
        11 month ago

        Yeah I tried that, Claude with some C code. Unfortunately the Ai only took me from point A to point A. And it only took a few hours :D

  • @Cold_Brew_Enema@lemmy.world
    link
    fedilink
    101 month ago

    Me with powershell. I’ll write a pretty complex script, not write powershell for 3 months, come back and have to completely relearn it.

  • synae[he/him]
    link
    fedilink
    English
    41 month ago

    Incredibly true for me these days. But don’t fret, shellcheck and tldp.org is all you need. And maybe that one stackoverflow answer about how to get the running script’s directory

    • @ewenak@jlai.lu
      link
      fedilink
      11 month ago

      If When the script gets too complicated, AI could also convert it to Python.

      I tried it once at least, and it did a pretty good job, although I had to tell it to use some dedicated libraries instead of calling programs with subprocess.

    • @henfredemars@infosec.pub
      link
      fedilink
      English
      61 month 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.

    • @SpaceNoodle@lemmy.world
      link
      fedilink
      17
      edit-2
      1 month 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
      201 month 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.

  • @wwb4itcgas@lemm.ee
    link
    fedilink
    English
    21 month ago

    I have a confession to make: Unless shell script is absolutely required, I just use Python for all my automation needs.

  • Victor
    link
    fedilink
    321 month 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!

    • 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
        link
        fedilink
        3
        edit-2
        1 month 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.

        • 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.

          • Victor
            link
            fedilink
            11 month ago

            Fish syntax is still fairly ugly compared to most programming languages in my opinion.

            subprocess.run(["fd", "-t", "d", "some_query"])
            

            vs

            fd -t d some_query
            

            Which is cleaner? Not to mention if you want to take the output from the command and pipe it into another one.

            It’s not about folks with weird opinions or otherwise, it’s about use cases. 🙂 I don’t think python is any more “natural” than most other imperative languages.

            Fish is probably even more natural, actually, due to it being more high level and the legibility of the script is basically dependent on the naming of the commands and options and variables used within it, rather than something else, just like python. They probably have similarly legible keywords. Fish I imagine has fewer, which is a good thing for legibility. A script does a lot more with a lot less, due to the commands themselves doing so much behind the scenes. There’s a lot more boilerplate to a “proper” programming language than a scripting language.

            But if you want to do something that python is better suited for, like advanced data processing or number crunching, or writing a whole application, then I would say that would be the better choice. It’s not about preference for me when it comes to python vs fish, it’s about the right tool for the job. But if we’re talking about bash vs fish, then I’m picking fish purely by preference. 👍

    • @raldone01@lemmy.world
      link
      fedilink
      6
      edit-2
      1 month ago

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

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

    • JackbyDev
      link
      fedilink
      English
      21 month ago

      It’s the default on CachyOS and I’ve been enjoying it. I typically use zsh.

      • Victor
        link
        fedilink
        11 month ago

        Yeah I also went bash -> zsh -> fish. Zsh was just too complicated to configure for my taste. Couldn’t do it, apart from copy pasting stuff I didn’t understand myself, and that just didn’t sit right.

    • @LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      21 month 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.

      • Victor
        link
        fedilink
        11 month ago

        Yeah, using one tool and then another one can be confusing at times. 😅

    • alt_xa_23
      link
      fedilink
      11 month ago

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

      • Victor
        link
        fedilink
        11 month ago

        Give it a shot after reading through the manual! (Extremely short compared to bash’s!) It’s a joy in my opinion. ☺️👌

  • @conditional_soup@lemm.ee
    link
    fedilink
    114
    edit-2
    1 month ago

    Regex

    Edit: to everyone who responded, I use regex infrequently enough that the knowledge never really crystalizes. By the time I need it for this one thing again, I haven’t touched it in like a year.

    • @LeninOnAPrayer@lemm.ee
      link
      fedilink
      English
      1
      edit-2
      1 month ago

      I just use the regex101 site. I don’t need anything too complicated ever. Has all the common syntax and shows matches as you type. Supports the different languages and globals.

    • FundMECFS
      link
      fedilink
      31 month ago

      This is one of the best uses for LLM’s imo. They do all my regex for me.

    • @Kissaki@programming.dev
      link
      fedilink
      English
      101 month 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
        161 month 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.

        • @Lehmanator@programming.dev
          link
          fedilink
          English
          11 month 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.

        • @Lehmanator@programming.dev
          link
          fedilink
          English
          11 month 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.

        • @Lehmanator@programming.dev
          link
          fedilink
          English
          11 month 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.

        • @Lehmanator@programming.dev
          link
          fedilink
          English
          31 month 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.

          • @ewenak@jlai.lu
            link
            fedilink
            01 month 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)

        • @activ8r@sh.itjust.works
          link
          fedilink
          English
          21 month 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.

  • katy ✨
    link
    fedilink
    261 month ago

    every control structure should end in the backwards spelling of how they started