108

Is $(...) the same as (...) in Bash?

Also, is $((...)) the same as ((...))?

Also, is ${...} the same as {...}?

More generally what does the dollar sign stand for? Thank you.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
John Robins
  • 1,427
  • 3
  • 11
  • 10
  • For a more comprehensive answer see http://tldp.org/LDP/abs/html/special-chars.html. For example $ in regex context means end of line. Generally $var gives the value of var. Considering var as a reference to (or name of) a value, such as in var=value, $var dereferences var to produce the value it references and can be considered as the dereference operator in contexts where it prefixes a reference. –  Jul 06 '15 at 21:37
  • 3
    `tldp.org` isn't a great source for Bash related things because the site has many errors and misleading information. – codeforester Mar 14 '18 at 19:18
  • Related post: [How to use double or single brackets, parentheses, curly braces](https://stackoverflow.com/questions/2188199/how-to-use-double-or-single-brackets-parentheses-curly-braces). – codeforester Mar 14 '18 at 19:18

2 Answers2

230

In the following, I use "returns" to indicate return values and "produces" to indicate "substitutes the resulting text."

  • $(...) means execute the command in the parens in a subshell and produces its stdout. Example:

      $ echo "The current date is $(date)"
      The current date is Mon Jul  6 14:27:59 PDT 2015
    
  • (...) means run the commands listed in the parens in a subshell. Example:

      $ a=1; (a=2; echo "inside: a=$a"); echo "outside: a=$a"
      inside: a=2
      outside: a=1
    
  • $((...)) means perform arithmetic and produce the result of the calculation. Example:

      $ a=$((2+3)); echo "a=$a"
      a=5
    
  • ((...)) means perform arithmetic, possibly changing the values of shell variables, but don't produce its result. Example:

      $ ((a=2+3)); echo "a=$a"
      a=5
    

    Note that the return value of the calculation is returned, so it can be used in while or if.

  • ${...} means produce the value of the shell variable named in the braces. Example:

      $ echo ${SHELL}
      /bin/bash
    
  • {...} means execute the commands in the braces as a group. Example:

      $ false || { echo "We failed"; exit 1; }
      We failed
    

More generally what does the dollar sign stand for?

It means whatever it means in the given context.

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
John1024
  • 109,961
  • 14
  • 137
  • 171
  • 13
    At least for `((...))` and `(...)`, you can think of the dollar-prefixed version as working the same as the statement, but expanding to the output (where the "output" of `((...))` is the arithmetic result. There's a more subtle analogy for `{...}`; in each case, they group their contents away from their surroundings. Think `${foo}_bar` vs `$foo_bar` and `echo foo; echo bar > /dev/null` vs `{ echo foo; echo bar; } > /dev/null`. – chepner Jul 06 '15 at 22:26
  • 5
    Excellent answer, except for that last paragraph: The `$` sign invariably signifies an *expansion*. I.e., based on what comes after the `$` (like `((`, `(`, `{`, etc.) some string value is computed *and the `$...` expression is replaced with the computed string*. – cmaster - reinstate monica Oct 08 '20 at 15:21
  • There is difference between _output_ and _returned result_. Purpose of `((...))` is to return result, but it does not print output and $((...)) prints output but does not return result. That's why ((...)) can be used as a condition: `if ((1 == 1)); then echo OK; fi` or `((1 == 1)) && echo OK`. Another difference, the ((...)) is bashism, $((...)) is POSIX – papo Nov 15 '21 at 10:33
  • Please check the edit `queue` if possible. – Artfaith May 30 '22 at 18:01
  • 1
    Note also that parens are also used to set the value of a variable as a bash array, e.g. `MYARRAY=(element1 element2 element3)` – Jeff Learman Jun 23 '22 at 13:23
  • Perhaps we could think of `$` as a dereferencing operation. For example `echo $var` and `echo $(func)` are bash equivalents to C's `*var` and `*func()`. Though I don't know if that approximation is consistent with `$'some\n odd \n string'` – Sridhar Sarnobat Sep 11 '22 at 23:57
  • I would also like to emphasize, that while commands in `() ` are executed in the subshell, commands in `{}` are executed in the current shell context (no subshell is created). `a=1; (a=2; echo "inside: a = $a"); echo "outside: a = $a"` gives us _inside: a = **2** outside: a = **1**_, and `a=1; { a=2; echo "inside: a= $a"; }; echo "outside: a=$a"` gives us _inside: a= **2** outside: a= **2**_. – Boolean_Type Jan 26 '23 at 22:36
6

Adding to the answer above:

  1. [..] is used in conditions or logical expressions. Example:
$ VAR=2
$ if [ $VAR -eq 2 ]

> then
> echo 'yes'
> fi
yes
  1. [[...]] offers extended functionality to single square brackets. Particularly, it is useful for =~ operator (used in regular expressions). Example:
$ VAR='some string'
$ if [[ $VAR =~ [a-z] ]]; then
> echo 'is alphabetic'
> fi
is alphabetic

Reference:

https://linuxconfig.org/bash-scripting-parenthesis-explained

Akshay
  • 73
  • 1
  • 8