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
    55
    ·
    2 years 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
      16
      ·
      2 years 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
        17
        ·
        2 years 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.

      • TimeSquirrel@kbin.social
        link
        fedilink
        arrow-up
        28
        ·
        2 years ago

        being autistic

        Easy there, a lot of people on the spectrum built everything you’re using to talk to me right now.

          • TimeSquirrel@kbin.social
            link
            fedilink
            arrow-up
            23
            ·
            edit-2
            2 years ago

            By “a lot of people”, I meant “a great many of them” compared to neurotypicals. Not all.

            It often takes a special kind of person to be able to absorb reams of dry technical knowledge in a narrow field and spit it out like it’s a second language.

            It’s easy to recognize in people like RMS, Steve Wozniak, and Torvalds if you are afflicted with it too (although technically none have been officially diagnosed). Even Elon Musk exhibits traits of it (as much as I don’t want to be associated with him) I can still recognize the complete social ineptitude and obsessive behaviors that are often associated with it.

    • hapidjus@mastodon.social
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      @TigrisMorte Why not? I don’t understand the hate with using valid filename characters in filenames. If anything I would argue it makes detecting non-conformant code easier… you wouldn’t want a program to skip processing a file because it has the letter Z in it would you?

      • TigrisMorte@kbin.social
        link
        fedilink
        arrow-up
        2
        ·
        2 years ago

        Makes CLI involving the space a pain. Use an underscore if you must have a visual space, but best practices would be to use Camel Case, no punctuation (including spaces), and include the date in Gregorian format CCYYMMDD

        I don’t know if any given system shall issues or could handle it fine, but I know some systems cannot handle spaces in file names. There is no reason to tempt fate.

          • TigrisMorte@kbin.social
            link
            fedilink
            arrow-up
            2
            ·
            2 years ago

            It is a filename not a PR press release. How it looks is irrelevant to how the system handles it. Want the display to show something other than the file name is easy and doesn’t risk errors, unlike punctuation in filenames which creates problems and solves none.

            • hapidjus@mastodon.social
              link
              fedilink
              arrow-up
              0
              ·
              2 years ago

              > How it looks is irrelevant to how the system handles it

              Then IMO it shouldn’t matter that there is a space in it. I disagree with your viewpoints and I think if someone wants a space or punctuation in their filename, they should be able to do that without problems.

              • TigrisMorte@kbin.social
                link
                fedilink
                arrow-up
                1
                ·
                2 years ago

                Your opinion does not matter. Nor does the presentation upon the screen, which is what you are actually asking for. The reality was explained and you are stamping your feet and holding your breath.

  • iusearchbtw@lemm.ee
    link
    fedilink
    arrow-up
    26
    ·
    2 years ago

    Am I not reading your question right? Just quote the variable and filenames with spaces work fine.

    for i in *; do
        cat "$i" ;
    done
    
  • gnuhaut@lemmy.ml
    link
    fedilink
    arrow-up
    17
    ·
    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
      4
      ·
      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?

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

  • eshep@social.trom.tf
    link
    fedilink
    arrow-up
    15
    ·
    edit-2
    2 years ago

    @barbara Is bash itself not already an improvement on ““the basic stuff””?

    …and whitespace in filenames is simply unacceptable, and should not be encouraged. 😆

    What’s wrong with the method we’ve been usin forever of working with dumbly named files? Just "enclose em", or use\ an\ escape\ char in em.

  • Responsabilidade@lemmy.eco.br
    link
    fedilink
    arrow-up
    8
    ·
    2 years ago

    Why don’t we improve the basic stuff, like processor architecture?

    Because if we do, we make everything we have working now break. So everything would need be ported to this new architecture.

    The same with bash or any other foundation lib.

    And also these “improvements” may make these libs more complex and, therefore, more unstable and hackable.

    The simple is simple for a reason: It works trustfully

  • kbal@fedia.ioBanned
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    2 years ago

    That’s quite a lot of comments so far with nobody saying
    IFS=’ ’

  • huf [he/him]@hexbear.net
    link
    fedilink
    English
    arrow-up
    5
    ·
    2 years ago

    i have bad news: bash is already a massively improved/extended ksh clone. ksh was a massively improved/extended sh clone. sh got a ton of improvements early on.

    this is about as good as you can get without breaking compatibility completely (bash already breaks compatibility with posix sh in some ways).

    anyway, once you’ve figured out the hermetic incantations required to work with filenames with whitespace in them, it’ll be time to write scripts that can handle filenames with newlines in them :D

    • palordrolap@kbin.social
      link
      fedilink
      arrow-up
      2
      ·
      2 years ago

      Most shells will issue $PS2 as the continuation prompt if you quote a filename and try to insert a carriage return.

      Ctrl-V Ctrl-J is the explicit keypress pair to insert a carriage return without triggering $PS2, but beware: If the carriage return is outside of quotes, that’s equivalent to starting a new command in much the same way a semicolon or a new line in a shell script would.

      echo "hello^V^Jthere" [Enter] echoes hello on one line and then there on the next, but echo hello^V^Jthere [Enter] will echo hello then try to run a command called there

      We’d have to assume that whatever fixes spaces in filenames would also have an option to fix this subtlety. And I say to whoever tries: Good luck with that.

  • rah@feddit.ukBanned
    link
    fedilink
    English
    arrow-up
    3
    ·
    2 years ago

    You tell us; you haven’t improved the basic stuff so you have the answer already in your own behaviour.