2

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
3

2 Answers 2

6

echo $a is the same as

echo backups cache crash lib local lock log mail opt run snap spool tmp

whereas echo "$a" is the same as

echo 'backups
cache
crash
lib
local
lock
log
mail
opt
run
snap
spool
tmp'

See https://mywiki.wooledge.org/Quotes.

The first argument for printf is a formatting string and printf $a is the same as printf backups cache crash lib local lock log mail opt run snap spool tmp so it's using the string backups as the format and discarding the rest since there's nothing like %s in the formatting string to use them in. Just like:

$ printf foo whatever
foo$

$ printf '%s\n' foo whatever
foo
whatever

Don't do a="$(ls)" to try to create a scalar variable holding file names btw as that's fragile, do a=(*) to hold them in an array instead.

12
  • Ok, what about the crippled echo in my example?
    – t7e
    Jun 21 at 23:05
  • echo's a different command, one that doesnt take a formatting string so it's just printing whatever you pass to it as arguments.
    – Ed Morton
    Jun 21 at 23:06
  • 1
    Yes it does, do declare -p a to see them. I updated my answer.
    – Ed Morton
    Jun 21 at 23:08
  • 1
    Just read the reference article I provide, it explains quotes very well.
    – Ed Morton
    Jun 21 at 23:14
  • 2
    @t7e In short, echo outputs its arguments with spaces between them. This is what echo does. When using echo $a, the shell splits the contents of $a into several arguments based on spaces, tabs and newlines (and then also does filename globbing on each generated word). echo then prints them with spaces between them. With echo "$a", you only ever give echo a single argument. The string "$a" contains newlines from the output of ls, and these are retained and outputted by echo.
    – Kusalananda
    Jun 22 at 4:41
1

Thanks to @Ed Morton and @Kusalananda for the explanation.
I guess my problem was that I always thought that, by default, ls splits the files using spaces or tabs. But in fact, it turned out that it separates them with new line characters but outputs the files in columns (sorted vertically) when printing to a terminal. Newline characters can be checked with:

ls | od -c

I'll move the @Kusalananda's answer from the comment section to an answer, since it was helpful:

In short, echo outputs its arguments with spaces between them. This is what echo does.
When using echo $a, the shell splits the contents of $a into several arguments based on spaces, tabs and newlines (and then also does filename globbing on each generated word). echo then prints them with spaces between them.
With echo "$a", you only ever give echo a single argument. The string "$a" contains newlines from the output of ls, and these are retained and outputted by echo.

3
  • 2
    "it separates them with new line characters but outputs the files in columns (sorted vertically)" -- The output of ls depends on if it prints to a terminal or to something else. Jun 22 at 9:36
  • to see the output with multiple filenames per line, you'd need to capture the output without it going somewhere other than a terminal. Something like running it under script, or ssh -t somehost ls > ls.txt, or maybe look at the system calls with strace, though it abbreviates long string arguments.
    – ilkkachu
    Jun 22 at 9:47
  • 1
    If you're interested in ls, see mywiki.wooledge.org/ParsingLs
    – Ed Morton
    Jun 22 at 11:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.