The output of
# ls
and
# echo $(ls)
are the same. What exactly does the $ sign and the parenthesis mean?
Also, what is going on a technical level that causes the output of these two commands to be the same?
The output of
# ls
and
# echo $(ls)
are the same. What exactly does the $ sign and the parenthesis mean?
Also, what is going on a technical level that causes the output of these two commands to be the same?
This is command substitution for bash.
http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution
More:
http://tldp.org/LDP/abs/html/commandsub.html
http://wiki.bash-hackers.org/syntax/expansion/cmdsubst
And a good example similar to your question:
http://bashshell.net/shell-scripts/using-command-substitution-in-a-bash-shell-script/
When the shell encounters text enclosed in
$(
)
, it:$(...)
expression. Any trailing newlines are removed from this output.$(...)
was not quoted (see below).As heartsmagic's answer explains, this is one of two available syntaxes for command substitution.
"What ... causes the output of these two commands to be the same?"
It's actually somewhat uncommon for the output of
ls
to be exactly the same as the output ofecho $(ls)
:ls
typically separates filenames by two or more spaces, or a newline. This helps us tell them apart more easily, especially since spaces in filenames are somewhat common (but multiple consecutive spaces, less common).Word Splitting
When I ran
echo $(ls)
, the following happened.Command substitution replaced
$(ls)
with:You might be surprised to hear that, since that's probably not what you see when you run
ls
by itself! This disparity in whatls
outputs is explained below, but is actually not the reason you end up with a single space between those two words. The same thing would happen if$(ls)
were substituted withbar foo
(with two spaces).Afterwards, the shell performed word splitting, treating
bar
andfoo
as separate arguments to theecho
command instead of as a single argument containing spaces.Globbing (a.k.a. filename/pathname expansion) would also be performed, if the command's output had contained
*
,?
, or[
.As detailed below, quoting with double quotes permits command substitution but suppresses word splitting and globbing.
When
echo
receives multiple arguments (except for options like-n
, which are treated specially and not printed at all), it prints them all out, with a single space between subsequent arguments.echo
then prints a newline, unless the-n
option was passed.Thus
echo $(ls)
shows output likebar foo
instead ofbar foo
.If you run
ls
and it lists no files, or just one file, the output ofls
will often be the same as the output ofecho $(ls)
. Even then, it will not always be the same, such as when a filename contains whitespace other than isolated (single) spaces:When
ls
prints multiple files, its output is not likely to be exactly that same as the output of$(ls)
. Similarly:echo a b
andecho $(echo a b)
produce the same output, butecho 'a b'
andecho $(echo 'a b')
do not.This is an important thing to know about command substitution--unquoted command substitutions are subject to word splitting (and globbing).
Quoting
If you want to prevent word splitting and globbing--and you usually will want to prevent them--you can enclose your expression for command substitution in double quotes (
"
"
). The reason to use double quotes rather than single quotes is that single quotes are even stronger; they would suppress command substitution.This also applies to parameter expansion, which is a more commonly used shell expansion than command substitution, and to arithmetic expansion. The reasons it is important to quote command substitutions (except in the fairly uncommon) case that you know you want further expansions to occur) are the same as the reasons to quote parameter expansion.
Terminal Detection
Double-quoting often sufficient to avoid unexpected results from command substitution. In particular, it will work with the
echo
-based examples above as well as the example ofls
on a directory with a single entry containing spaces:However, as noted above, you might be surprised to find that what you see when you run
ls
is often not what gets passed toecho
in place of"$(ls)"
:This is because
ls
checks if standard output is a terminal to decide how to format its output, when output formatting is not explicitly specified.ls
outputs in vertically sorted columns, likels -C
.ls
lists each entry on its own line, likels -1
.In command substitution, standard output isn't a terminal because the command's output is not being sent directly to a terminal for you to see--instead, it is being captured by the shell and used as part of another command.
To get multi-column formatting when running
ls
via command substitution, pass it the-C
flag:(
dir
is sometimes suggested as an alternative tols -C
and will also work for this, thoughdir
behaves likels -C -b
rather than merelyls -C
.)Shell Aliasing
Another reason
ls
may sometimes behave differently fromecho "$(ls)"
is thatls
may be a shell alias. Runalias ls
to check; on Ubuntu you usually getalias ls='ls --color=auto'
. This makes it so that, whenls
appears as the first word of a command you run interactively (and also in the far less common situation that alias expansion has been enabled in a non-interactive shell), it is replaced withls --color=auto
.For example, when I run
ls
, it lists directories colored blue and executables colored green (and observes many other coloring rules, too).--color=auto
causesls
to print colored output when standard output is a terminal, and not otherwise.To get colored output when running
ls
via command substitution, pass it the--color
or--color=always
option. If you like, you can combine this with-C
:Note that while you can make
ls
an alias tols --color
orls --color=always
instead ofls --color=auto
, andls --color=always
doesn't care if standard output is a terminal... that alias will still not causels
to produce colored output when invoked by command substitution. This is because shell aliases (in Bourne-style shells like bash) are only expanded when the shell sees them as the first word of a command, and they are not inherited by subshells.()
and$()
are used to run commands in a subshell.See http://tldp.org/LDP/abs/html/subshells.html for details.
Using $ in front of a variable name invokes the value assigned to the variable. Echo without the $ will just print the name of the variable to the screen (or standard out).