• Word splitting oddities (Was: Long filenames in DOS/Windows and Unix/Li

    From Kenny McCormack@21:1/5 to candycanearter07@candycanearter07.n on Sat Sep 7 21:51:29 2024
    In article <slrnvdp692.1s5hi.candycanearter07@candydeb.host.invalid>, candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> wrote:
    ...
    Only sometimes it doesn't :-)

    $ foo="foo bar"
    $ bar=$foo

    No problem with the space in the expanded value here!

    $ echo $bar
    foo bar

    but of course:
    ls $bar
    ls: cannot access 'foo': No such file or directory
    ls: cannot access 'bar': No such file or directory

    Fair point. Field splitting happens after parameter expansion, except
    when it doesnt.


    Actually, echo is special since it just prints every argument it's
    given.. both of these commands are working as expected with splitting:

    $ echo some text

    some text

    $ ls some text

    ls: cannot access 'some': No such file or directory
    ls: cannot access 'text': No such file or directory

    The point is still valid, even if the demonstration wasn't quite on-point.

    When faced with the problem of needing to display a variable in shell, but needing to know whether or not it is a single string or multiple strings, a useful trick is to use "printf" like this:

    $ set -- this is "a test of" something
    $ printf "%s\n" "$@"

    Then you would know for sure that in the example above, the variable "foo" really did have the value "foo bar".

    Note, incidentally, that in the example above, the statement: bar=$foo
    pretty much has to be parsed as if $foo had been quoted, because the alternative is much worse. The alternative would be to interpret:

    bar=$foo
    as
    bar=foo bar

    which means to run the "bar" command with the env var "bar" set to "foo".

    --
    Trump has normalized hate.

    The media has normalized Trump.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)