Difference between ${} and $() in a shell script

93,729

Solution 1

$(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 $().

Solution 2

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.

Solution 3

I usually see it more commonly in strings. Something like this will not work:

var="a"
echo "$varRAW_STRING"

But this will:

var="a"
echo "${var}RAW_STRING"

As you correctly said, $() is used to execute a command:

dir_contents=$(ls)

You can also use backticks, but I find the $() more versatile. For one thing, backticks cannot be (easily) nested.

date_directory=`ls `date '+%Y-%m-%d'`` # Makes no sense
date_directory=$(ls $(date '+%Y-%m-%d')) # Much better
Share:
93,729

Related videos on Youtube

Noob
Author by

Noob

Updated on September 18, 2022

Comments

  • Noob
    Noob almost 2 years
    $ 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?

  • Noob
    Noob almost 9 years
    thanks for the wonderful example. Can you elaborate the use of ! in your example. I don't really understand how does it work.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    @Noob: OK, I elaborated on the use of !.
  • Noob
    Noob almost 9 years
    thanks. Somehow !animal is actually referring to the variable cat instead of the value cat. Can you also let me know how does /at become a "c" in echo ${animal/at/ow} The man explanation of bash is somehow... i don't know. hard to understand.
  • Noob
    Noob almost 9 years
    also, can you elaborate this phrase - "${parameter} The value of parameter is substituted." substituted with what ? if parameter is fruit and its value is apple - fruit=apple, so ${fruit} - apple is substituted with ?? don't really get the meaning substituted here
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    Actually, backticks can be nested: date_directory=`ls \`date '+%Y-%m-%d'\``.  But it’s terribly ugly; $(…) is much clearer and easier to use.
  • bytesized
    bytesized almost 9 years
    Ok, fair enough. It is technically possible, but I cannot imagine anyone ever doing it just because of the readability
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    Well, `…` was the (only) syntax for command substitution for years before $(…) was invented, so you don't need to imagine anything — people did it.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    @Noob: “${!animal} is actually referring to the variable $cat instead of the value cat.”  Yes, that’s exactly the point.  “how does /at become a "c" in echo ${animal/at/ow}?”  Huh?  /at doesn’t become "c"; “cat” becomes “cow” when “at” is replaced with “ow”.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' almost 9 years
    @Noob: “Also, can you elaborate this phrase: "${parameter} — The value of parameter is substituted."?”  You already understand it.  When you say echo ${name}, foo is substituted for ${name}, so it’s as if you had typed echo foo.  As a slightly more realistic/practical example, nursery rhyme.txt is substituted for $filename, so ls -ld "$filename" is equivalent to ls -ld "nursery rhyme.txt".