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

  • OpenStars@discuss.online
    link
    fedilink
    English
    arrow-up
    0
    ·
    6 months ago

    It has nothing to do with bash specifically - other shells like sh, csh, tcsh, zsh, etc. are the same. Whitespace in UNIX is just that way by design. And it’s been a long while since I used a Windows CLI but they were that way too - plus added all that weirdness about ~1 at the ends of filenames, and Mac OSX also. So not even just UNIX, but it’s how the CLIs tend to work, where whitespace acts as the “delimiter” between arguments sent to a program.

    program_name arg1 arg2 arg3 arg4

    So if you use whitespace like “cp file 1 file 2”, the CLI sends arg1=“file”, arg2=“1”, arg3=“file”, arg4=“2”, rather than arg1=“file 1” and arg2=“file 2”. These are just the foundational rules of how CLIs work - a computer can’t read your mind, and this is how you precisely tell it what you want, within this highly rigid framework to avoid misunderstandings.

    The alternative is to use a GUI, so like see file, drag file, and ofc that has its own set of tradeoffs good and bad.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      0
      ·
      6 months ago

      Yeah, for this reason, lots of full-fledged programming languages actually make you specify the arguments as a list of strings directly, so for example:

      Command::new("cp")
          .args(["file 1", "file 2”])
      
      • OpenStars@discuss.online
        link
        fedilink
        English
        arrow-up
        0
        ·
        6 months ago

        It’s a subset of the standard delimiter problem: if I want to use the delimiter inside of an entry, can I even do that and if so then how?

        e.g. in comma-delimited lists you could “escape” the commas individually, or encapsulate each entry inside quotes, or provide each entry by name, etc. - all of which significantly complicates the retrieval process by adding greater complexity to decide on rules determining how it all works (like if by name, then what if the user [stupidly? on purpose?] provides multiple entries with the same name - do subsequent ones overwrite the earlier ones or their contents get appended to the end and if the latter, is any separation provided between them? and on and on it goes):

        • item1,item2,item3
        • “Denver, CO”,“New York, NY”,Miami/, FL
        • “Lastname, Firstname”,Lastname/, Firstname
        • item1=“Denver, CO”, item2=“New York, NY”

        Common English has issues with this too like is a list with “John, Marsha, Barbie and Ken” 4 entries or just 3 where the latter is a pairing? (leading to Oxford comma discussion:-P it is very important though bc while while individual people may have similar needs like food, pairings may have different constraints like if they drive together then they need less parking space)

        So this delimiter issue is not even specific to CLIs, nor even computers in general - it is a universal problem with any communication system.

  • gnuhaut@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    6 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.