Path: ...!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail From: gazelle@shell.xmission.com (Kenny McCormack) Newsgroups: comp.unix.programmer Subject: Word splitting oddities (Was: Long filenames in DOS/Windows and Unix/Linux) Date: Sat, 7 Sep 2024 21:51:29 -0000 (UTC) Organization: The official candy of the new Millennium Message-ID: References: <9e7a4bd1-bfbb-4df7-af1a-27ca9625e50bn@googlegroups.com> Injection-Date: Sat, 7 Sep 2024 21:51:29 -0000 (UTC) Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4"; logging-data="1799353"; mail-complaints-to="abuse@xmission.com" X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Originator: gazelle@shell.xmission.com (Kenny McCormack) Bytes: 2762 Lines: 60 In article , candycanearter07 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.