• JTskulk@lemmy.world
    link
    fedilink
    English
    arrow-up
    12
    ·
    15 hours 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

  • LeninOnAPrayer@lemm.ee
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    16 hours 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
      6
      ·
      15 hours 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
        2
        ·
        14 hours 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.

  • jkercher@programming.dev
    link
    fedilink
    English
    arrow-up
    7
    ·
    19 hours 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.

  • 6mementomorib@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    9
    ·
    21 hours ago

    i used powershell, and even after trying every other shell and as a die hard Linux user I’ve considered going back to powershell cause damn man

    • ronflex@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      19 hours ago

      I am a huge fan of using PowerShell for scripting on Linux. I use it a ton on Windows already and it allows me to write damn near cross-platform scripts with no extra effort. I still usually use a Bash or Fish shell but for scripting I love being able to utilize powershell.

    • HyperMegaNet@lemm.ee
      link
      fedilink
      arrow-up
      7
      ·
      24 hours ago

      Thank you for this. About a year ago I came across ShellCheck thanks to a comment just like this on Reddit. I also happened to be getting towards the end of a project which included hundreds of lines of shell scripts across dozens of files.

      It turns out that despite my workplace having done quite a bit of shell scripting for previous projects, no one had heard about Shell Check. We had been using similar analysis tools for other languages but nothing for shell scripts. As you say, it turned up a huge number of errors, including some pretty spicy ones when we first started using it. It was genuinely surprising to see how many unique and terrible ways the scripts could have failed.

    • ethancedwards8@programming.dev
      link
      fedilink
      English
      arrow-up
      6
      ·
      1 day ago

      I wish it had a more comprehensive auto correct feature. I maintain a huge bash repository and have tried to use it, and it common makes mistakes. None of us maintainers have time to rewrite the scripts to match standards.

      • I honestly think autocorrecting your scripts would do more harm than good. ShellCheck tells you about potential issues, but It’s up to you to determine the correct behavior.

        For example, how could it know whether cat $foo should be cat "$foo", or whether the script actually relies on word splitting? It’s possible that $foo intentionally contains multiple paths.

        Maybe there are autofixable errors I’m not thinking of.

        FYI, it’s possible to gradually adopt ShellCheck by setting --severity=error and working your way down to warnings and so on. Alternatively, you can add one-off #shellcheck ignore SC1234 comments before offending lines to silence warnings.

        • UndercoverUlrikHD@programming.dev
          link
          fedilink
          arrow-up
          4
          ·
          20 hours ago

          For example, how could it know whether cat $foo should be cat "$foo", or whether the script actually relies on word splitting? It’s possible that $foo intentionally contains multiple paths.

          Last time I used ShellCheck (yesterday funnily enough) I had written ports+=($(get_elixir_ports)) to split the input since get_elixir_ports returns a string of space separated ports. It worked exactly as intended, but ShellCheck still recommended to make the splitting explicit rather than implicit.

          The ShellCheck docs recommended

          IFS=" " read -r -a elixir_ports <<< "(get_elixir_ports)"
          ports+=("${elixir_ports[@]}")
          
      • stetech@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        1 day ago

        Then you’ll have to find the time later when this leads to bugs. If you write against bash while declaring it POSIX shell, but then a random system’s sh doesn’t implement a certain thing, you’ll be SOL. Or what exactly do you mean by “match standards”?

  • Victor@lemmy.world
    link
    fedilink
    arrow-up
    30
    ·
    1 day 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
      2
      ·
      16 hours 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
      9
      ·
      22 hours 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
        3
        ·
        edit-2
        15 hours 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
          1
          ·
          13 hours 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.

          • Victor@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            8 hours 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
      arrow-up
      6
      ·
      edit-2
      9 hours 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.

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        15 hours 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.

    • alt_xa_23@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      20 hours ago

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

      • Victor@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        15 hours ago

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

    • JackbyDev@programming.dev
      link
      fedilink
      English
      arrow-up
      6
      ·
      20 hours ago

      Everything is text! And different programs output in different styles. And certain programs can only read certain styles. And certain programs can only convert from some into others. And don’t get me started on IFS.

  • conditional_soup@lemm.ee
    link
    fedilink
    arrow-up
    114
    arrow-down
    9
    ·
    edit-2
    1 day 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
      arrow-up
      1
      ·
      edit-2
      16 hours 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.

    • dirtycrow@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      16 hours ago

      For me I spent one hour of ADHD hyper focusing to get the gist of regex. Python.org has good documentation. It’s been like 2 years so I’ve forgotten it too lol.

    • 9point6@lemmy.world
      link
      fedilink
      arrow-up
      37
      ·
      1 day 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
      32
      arrow-down
      1
      ·
      1 day ago

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

        • activ8r@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          1
          ·
          13 hours 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.

          • TheEighthDoctor@lemmy.zip
            link
            fedilink
            English
            arrow-up
            1
            ·
            4 hours ago

            I have no interest in learning regex ever in my life, I have better things to dedicate my brain capacity to haha

    • Kissaki@programming.dev
      link
      fedilink
      English
      arrow-up
      13
      arrow-down
      1
      ·
      1 day 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
        14
        ·
        1 day 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
          arrow-up
          2
          ·
          3 hours 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
          arrow-up
          1
          ·
          3 hours 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
          arrow-up
          1
          ·
          3 hours 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
          arrow-up
          1
          ·
          3 hours 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
            arrow-up
            1
            arrow-down
            1
            ·
            1 day 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)

    • cm0002@lemmy.worldOP
      link
      fedilink
      arrow-up
      5
      ·
      1 day ago

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

  • coldsideofyourpillow@lemmy.cafe
    link
    fedilink
    English
    arrow-up
    20
    arrow-down
    1
    ·
    edit-2
    1 day ago

    That’s why I use nushell. Very convenient for writing scripts that you can understand. Obviously, it cannot beat Python in terms of prototyping, but at least I don’t have to relearn it everytime.

    • AnUnusualRelic@lemmy.world
      link
      fedilink
      English
      arrow-up
      23
      ·
      1 day ago

      So the alternative is:

      • either an obtuse script that works everywhere, or
      • a legible script that only works on your machine…
      • shortrounddev@lemmy.world
        link
        fedilink
        English
        arrow-up
        6
        ·
        1 day ago

        I am of the opinion that production software shouldn’t be written in shell languages. If it’s something which needs to be redistributed, I would write it in python or something

        • coldsideofyourpillow@lemmy.cafe
          link
          fedilink
          English
          arrow-up
          1
          ·
          12 hours ago

          On a more serious note, NOTHING with more than a little complexity should be written in shell scripts imo. For that, Python is the best, primarily due to how fast it is to prototype stuff in it.

        • Hexarei@programming.dev
          link
          fedilink
          arrow-up
          3
          ·
          23 hours ago

          I tend to write anything for distribution in Rust or something that compiles to a standalone binary. Python does not an easily redistributable application make lol

          • shortrounddev@lemmy.world
            link
            fedilink
            English
            arrow-up
            2
            ·
            edit-2
            23 hours ago

            Yeah but then you either need to compile and redistribute binaries for several platforms, or make sure that each target user has rust/cargo installed. Plus some devs don’t trust compiled binaries in something like an npm package

        • AnUnusualRelic@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          1 day ago

          For a bit of glue, a shell script is fine. A start script, some small utility gadget…

          With python, you’re not even sure that the right version is installed unless you ship it with the script.

    • Akito@lemmy.zip
      link
      fedilink
      English
      arrow-up
      12
      ·
      1 day ago

      Nu is great. Using it since many years. Clearly superior shell. Only problem is, that it constantly faces breaking changes and you therefore need to frequently update your modules.

        • Akito@lemmy.zip
          link
          fedilink
          English
          arrow-up
          3
          ·
          1 day ago

          Yesterday, I upgraded from 0.101.0 to 0.102.0 and date to-table was replaced equally (actually better) with into record, however it was not documented well in the error. Had to research for 5 to 10 minutes, which does not sound much, but if you get this like every second version, the amount of time adds up quickly.

            • Akito@lemmy.zip
              link
              fedilink
              English
              arrow-up
              1
              ·
              1 day ago

              Yes, I switched to an older version and there was the warning. However, there was no warning on 0.101.0 whatsoever, so upgrading just one patch version broke my master module.

              Sometimes, I skip some versions, so I am certain, that I jumped from < 0.100.0 straight to 0.101.0 and here we are, without any deprecation warning.

        • barsoap@lemm.ee
          link
          fedilink
          arrow-up
          4
          ·
          edit-2
          1 day ago

          Not really. They’ve been on the stabilising path for about two years now, removing stuff like dataframes from the default feature set to be able to focus on stabilising the whole core language, but 1.0 isn’t out yet and the minor version just went three digits.

          And it’s good that way. The POSIX CLI is a clusterfuck because it got standardised before it got stabilised. dd’s syntax is just the peak of the iceberg, there, you gotta take out the nail scissors and manicure the whole lawn before promising that things won’t change.

          Even in its current state it’s probably less work for many scripts, though. That is, updating things, especially if you version-lock (hello, nixos) will be less of a headache than writing sh could ever be. nushell is a really nice language, occasionally a bit verbose but never in the boilerplate for boilerplate’s sake way, but in the “In two weeks I’ll be glad it’s not perl” way. Things like command line parsing are ludicrously convenient (though please nushell people land collecting repeated arguments into lists).

          • Akito@lemmy.zip
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 day ago

            Fully agree on this. I do not say, it’s bad. I love innovation and this is what I love about Nushell. Just saying, that using it at work might not always be the best idea. ;)

    • expr@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      1 day ago

      We have someone at work who uses it and he’s constantly having tooling issues due to compatibility problems, so… yeah.

      I’m sure it’s fine for sticking in the shebang and writing your own one-off personal scripts, but I would never actually main it. Too much ecosystem relies on bash/posix stuff.