Explanation for newbies:

  • Shell is the programming language that you use when you open a terminal on linux or mac os. Well, actually “shell” is a family of languages with many different implementations (bash, dash, ash, zsh, ksh, fish, …)

  • Writing programs in shell (called “shell scripts”) is a harrowing experience because the language is optimized for interactive use at a terminal, not writing extensive applications

  • The two lines in the meme change the shell’s behavior to be slightly less headache-inducing for the programmer:

    • set -euo pipefail is the short form of the following three commands:
      • set -e: exit on the first command that fails, rather than plowing through ignoring all errors
      • set -u: treat references to undefined variables as errors
      • set -o pipefail: If a command piped into another command fails, treat that as an error
    • export LC_ALL=C tells other programs to not do weird things depending on locale. For example, it forces seq to output numbers with a period as the decimal separator, even on systems where coma is the default decimal separator (russian, dutch, etc.).
  • The title text references “posix”, which is a document that standardizes, among other things, what features a shell must have. Posix does not require a shell to implement pipefail, so if you want your script to run on as many different platforms as possible, then you cannot use that feature.

  • Phoenix3875@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    16 days ago

    A lot of people call set -euo pipefail the “strict mode” for bash programming, as a reference to "use strict"; in JavaScript.

    In other words, always add this if you want to stay sane unless you’re a shellcheck user.

        • panicnow@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          14 days ago

          I’m so used to using powershell to handle collections and pipelines that I find I want it for small scripts on Mac. For instance, I was using ffmpeg to alter a collection of files on my Mac recently. I found it super simple to use Powershell to handle the logic. I could have used other tools, but I didn’t find anything about it terrible.

  • LordPassionFruit@lemm.ee
    link
    fedilink
    arrow-up
    4
    ·
    16 days ago

    I have written 5 shell scripts ever, and only 1 of them has been more complex than “I want to alias this single command”

    I can’t imagine being an actual shell dev

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      16 days ago

      only 1 of them has been more complex than “I want to alias this single command”

      I have some literal shell aliases that took me hours to debug…

  • jkercher@programming.dev
    link
    fedilink
    English
    arrow-up
    4
    ·
    15 days ago

    I was never a fan of set -e. I prefer to do my own error handling. But, I never understood why pipefail wasn’t the default. A failure is a failure. I would like to know about it!

    • owsei@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      15 days ago

      IIRC if you pipe something do head it will stop reading after some lines and close the pipe, leading to a pipe fail even if everything works correctly

      • jkercher@programming.dev
        link
        fedilink
        English
        arrow-up
        3
        ·
        15 days ago

        Yeah, I had a silly hack for that. I don’t remember what it was. It’s been 3-4 years since I wrote bash for a living. While not perfect, I still need to know if a pipeline command failed. Continuing a script after an invisible error, in many cases, could have been catastrophic.

  • tetris11@lemmy.ml
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    edit-2
    15 days ago

    My only issue is -u. How do you print help text if your required parameters are always filled. There’s no way to test for -z if the shell bails on the first line.

    Edit: though I guess you could initialise your vars with bad defaults, and test for those.

  • smeg@feddit.uk
    link
    fedilink
    English
    arrow-up
    0
    ·
    16 days ago

    Shell is great, but if you’re using it as a programming language then you’re going to have a bad time. It’s great for scripting, but if you find yourself actually programming in it then save yourself the headache and use an actual language!

    • renzev@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      0
      ·
      edit-2
      16 days ago

      Alpine linux, one of the most popular distros to use inside docker containers (and arguably good for desktop, servers, and embedded) is held together by shell scripts, and it’s doing just fine. The installer, helper commands, and init scripts are all written for busybox sh. But I guess that falls under “scripting” by your definition.