Difference between ${} and $() in a shell script
$ echo $(date)
Thu Jul 2 16:33:11 SGT 2015
$ echo ${date}
$ name=foo
$ echo $(name)
ksh: name: not found
$ echo ${name}
foo
Seems like ${variable}
is the same as $variable
, while $()
is to execute a command. Why use ${}
then?
$(command)
is “command substitution”.
As you seem to understand, it runs the command
, captures its output,
and inserts that into the command line that contains the $(…)
; e.g.,
$ ls -ld $(date +%B).txt
-rwxr-xr-x 1 Noob Noob 867 Jul 2 11:09 July.txt
${parameter}
is “parameter substitution”.
A lot of information can be found in the shell’s man page, bash(1),
under the “Parameter Expansion” heading:
${parameter}
The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character which is not to be interpreted as part of its name.
For positional parameters, see “Positional Parameters”, below.
In its most common usage, as shown in the other answers,
parameter
is a variable name.
The ${…}
form, as stated at the end of the paragraph above,
lets you get the value of a variable
(i.e., $variable_name
)
and follow it immediately with a letter, digit, or underscore:
$ animal=cat $ echo $animals # No such variable as “animals”. $ echo ${animal}s cats $ echo $animal_food # No such variable as “animal_food”. $ echo ${animal}_food cat_food
You can also do this with quotes:
$ echo "$animal"s cats
Or, as an exercise in options, you could use a second variable:
$ plural=s $ echo $animal$plural cats
But this is just step 1. The next paragraph in the man page is interesting, although a little cryptic:
If the first character of parameter
is an exclamation point (!
),
a level of variable indirection is introduced.
Bash uses the value of the variable formed from the rest of parameter
as the name of the variable; this variable is then expanded
and that value is used in the rest of the substitution,
rather than the value of parameter itself.
This is known as indirect expansion.
…(exceptions)…
The exclamation point must immediately follow the left brace
in order to introduce indirection.
I’m not sure how I can make this clearer except by example:
$ animal=cat $ echo $animal cat $ cat=tabby $ echo $cat tabby $ echo ${!animal} tabby # If $animal is “cat”, then ${!animal} is $cat, i.e., “tabby”
So let’s call that step 1½. There are lots of interesting things that you can do as step 2:
$ animal=cat $ echo ${#animal} 3 # String length $ echo ${animal/at/ow} cow # Substitution
You can’t do any of those things without the {
…}
braces.
Positional Parameters
Consider this artificial example:
$ cat myecho.sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $ ./myecho.sh Hey diddle diddle, The cat and the fiddle, The cow jumped over the moon. Hey diddle diddle, The cat and the fiddle, The Hey0 Hey1 Hey2 Hey3 Hey4 Hey5
because the shell doesn’t understand $10
, $11
, etc.
It treats $10
as if it were ${1}0
.
But it does understand ${10}
, ${11}
, etc., as mentioned in the man page
(“a positional parameter with more than one digit”).
But don’t actually write scripts like that; there are better ways to deal with long argument lists.
The above (along with many more forms
of ${parameter…something_else}
constructs)
are discussed at greater length in the shell’s man page, bash(1).
A Note on Quotes
Note that you should always quote shell variables unless you have a good reason not to, and you’re sure you know what you’re doing. By contrast, while braces can be important, they’re not as important as quotes.
$ filename="nursery rhyme.txt" $ ls -ld ${filename} ls: cannot access nursery: No such file or directory ls: cannot access rhyme.txt: No such file or directory $ ls -ld "$filename" -rwxr-xr-x 1 Noob Noob 5309 Jul 2 11:09 nursery rhyme.txt
That also applies to positional parameters
(i.e., command-line arguments; e.g., "$1"
) and also command substitution:
$ ls -ld $(date "+%B %Y").txt ls: cannot access July: No such file or directory ls: cannot access 2015.txt: No such file or directory $ ls -ld "$(date "+%B %Y").txt" -rwxr-xr-x 1 Noob Noob 687 Jul 2 11:09 July 2015.txt
See Bash quotes unescaped on command substitution
for a brief treatise on the interaction between quotes and $(
…)
.
In your example, $var and ${var} are identical. However, curly braces are useful when you wish to expand the variable in a string:
$ string=foo
$ echo ${string}bar
foobar
$ echo $stringbar
$
Thus the curly braces provide a means of substituting the variable in order to get the name of new variable, itself to be substituted.