$ echo $(date)
Thu Jul 2 16:33:11 SGT 2015
$ echo ${date}

$ name=foo
$ echo $(name)
ksh: name:  not found

$ echo ${name}

Seems like ${variable} is the same as $variable, while $() is to execute a command. Why use ${} then?


3 Answers 3


$(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:

    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
$ echo $animal_food
                                # No such variable as “animal_food”.
$ echo ${animal}_food

You can also do this with quotes:

$ echo "$animal"s

Or, as an exercise in options, you could use a second variable:

$ plural=s
$ echo $animal$plural

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=tabby
$ echo $cat
$ 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 $().

  • thanks for the wonderful example. Can you elaborate the use of ! in your example. I don't really understand how does it work.
    – Noob
    Commented Jul 9, 2015 at 12:53
  • @Noob: OK, I elaborated on the use of !. Commented Jul 9, 2015 at 15:39
  • 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
    Commented Jul 9, 2015 at 16:52
  • 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
    – Noob
    Commented Jul 9, 2015 at 16:57
  • @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”. Commented Jul 9, 2015 at 17:17

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


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

echo "$varRAW_STRING"

But this will:

echo "${var}RAW_STRING"

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


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
  • Actually, backticks can be nested: date_directory=`ls \`date '+%Y-%m-%d'\``.  But it’s terribly ugly; $(…) is much clearer and easier to use. Commented Jul 9, 2015 at 17:02
  • Ok, fair enough. It is technically possible, but I cannot imagine anyone ever doing it just because of the readability
    – bytesized
    Commented Jul 9, 2015 at 17:03
  • 1
    Well, `…` was the (only) syntax for command substitution for years before $(…) was invented, so you don't need to imagine anything — people did it. Commented Jul 9, 2015 at 17:08
  • s/ever doing it/doing it anymore/
    – bytesized
    Commented Jul 9, 2015 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .