Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double parenthesis with and without dollar

Tags:

bash

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.

like image 697
John Robins Avatar asked Jul 06 '15 21:07

John Robins


People also ask

What does a double parentheses mean?

Multiple parentheses—or the "echo," as it is sometimes referred to—is a typographical practice used by some anti-Semites on-line. It typically consists of three pairs of parentheses or brackets used around someone's name or around a term or phrase.

What is the purpose of the double parentheses (( )) in the scripts?

Similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation. In its simplest form, a=$(( 5 + 3 )) would set a to 5 + 3, or 8.

How do you type double parentheses?

1. Use brackets inside parentheses to create a double enclosure in the text. Avoid parentheses within parentheses, or nested parentheses. Correct: (We also administered the Beck Depression Inventory [BDI; Beck, Steer, & Garbin, 1988], but those results are not reported here.)

Can you use double parentheses?

2. Separate citations from parenthetical text with either semicolons (for parenthetical-style citations) or commas around the year (for narrative citations). Do not use a double enclosure or back-to-back parentheses.


2 Answers

  • $(...) means execute the command in the parens in a subshell and return 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 return 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 return its result. Example:

    $ ((a=2+3)); echo "a=$a" a=5 
  • ${...} means return 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.

like image 92
John1024 Avatar answered Sep 29 '22 10:09

John1024


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

like image 44
Akshay Avatar answered Sep 29 '22 10:09

Akshay