I know that there are ten different alternatives. Why don’t we simply improve the basic stuff?

  • gnuhaut@lemmy.ml
    link
    fedilink
    arrow-up
    15
    arrow-down
    1
    ·
    8 months ago

    As others have said, if you quote your variables, they won’t get split on spaces. The Unix shell unfortunately has ton of gotchas like this, and the reason this is not changed is backwards-compatibility. Lots of shell scripts depend on this behavior, e.g. there might be something like:

    flags="-a -l"
    ls $flags
    

    If you quote this (ls "$flags"), ls will see it as one argument, instead of splitting it into two arguments. You could patch the shell to not split arguments by default, and invent some other syntax for when you want this splitting behavior, but that would break a ton of existing shell scripts, and confuse users who are already familiar with the way it works right now. It would also make the shell incompatible with other shells, and violate the POSIX standard.

    • taladar@sh.itjust.works
      link
      fedilink
      arrow-up
      4
      arrow-down
      1
      ·
      8 months ago

      The reason for this is not backwards compatibility, the reason is that it would be stupid. Space appears a lot more often in situations where you need a separator than in filenames so why would you make the common case harder to use to save some typing in the edge case?

      • gnuhaut@lemmy.ml
        link
        fedilink
        arrow-up
        3
        arrow-down
        1
        ·
        8 months ago

        I disagree. The vast majority of the time when writing shell scripts, I quote variables, because that’s almost always what I want. Splitting is basically only useful if you have a list of arguments, and you know for sure there are no spaces in any of the arguments (so no filenames).

        (The workarounds in pure POSIX shell are btw super annoying if you want to pass a list arguments that may have spaces in them: You can abuse the special "$@" variable. Or you could probably also construct something with xargs.)

        • taladar@sh.itjust.works
          link
          fedilink
          arrow-up
          3
          ·
          edit-2
          8 months ago

          Every single command, option and argument in the shell is split by spaces, regardless of what it contains. That is clearly the more common case. I am not talking about splitting when the space comes out of a variable but in general, as part of the syntax.

          I am well aware of how quoting works to avoid accidental splitting and it is an absolute non-issue in practice once you get used to quoting things, about as annoying as the fact that you have to quote strings in every other programming language, i.e. not at all.

          • gnuhaut@lemmy.ml
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            8 months ago

            Ah that’s your point. Yeah I agree that splitting literal a b c is convenient. It is surprising to many (like here) that this happens after variable substitution, and that’s not very convenient since you almost never want that. You could define this to happen the other way around, but then you’d obviously have to invent a new syntax for explicit splitting, which would be its own kind of annoying.

            Edit: YSH (oil) does that btw. See here.