Sujet : Word splitting oddities (Was: Long filenames in DOS/Windows and Unix/Linux)
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.programmerDate : 07. Sep 2024, 22:51:29
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <vbiht0$1mt5p$1@news.xmission.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
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.