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

  • gnuhaut@lemmy.ml
    link
    fedilink
    arrow-up
    9
    ·
    2 years 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
      2
      arrow-down
      1
      ·
      2 years 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?