I have the following example:
$ a="$(ls)"
$ echo $a
backups cache crash lib local lock log mail opt run snap spool tmp
$
$ echo "$a"
backups
cache
crash
lib
local
lock
log
mail
opt
run
snap
spool
tmp
Now with printf
:
$ printf $a
backups
$
$ printf "$a"
backups
cache
crash
lib
local
lock
log
mail
opt
run
snap
spool
tmp
Why is the output so different? What do quotes do in this situation? Could someone explain what's going on here?
P.S. Found some explanation on the ls
behavior:
Output from ls has newlines but displays on a single line. Why?
https://superuser.com/questions/424246/what-is-the-magic-separator-between-filenames-in-ls-output
http://mywiki.wooledge.org/ParsingLs
The newline characters can be checked this way:
ls | od -c
ls
outputs differently to a terminal vs. to a non-terminal is different from the issue of word-splitting that you're seeing withecho $a
vs.echo "$a"
. You could change that command substitution toa=$(printf 'foo\nbar\n')
(ora=$(seq 3)
, or...) and you'd get the same issue wrt. the quoted vs. unquoted expansion. – ilkkachu Jun 22 '22 at 09:30echo $variable
shows something else". – Gordon Davisson Jun 22 '22 at 09:44