Using parenthesis to group and expand expressions

Grouping a (list of commands) in parenthesis causes them to be executed as if they were a single unit.

When commands are grouped, redirections can be applied to the entire command list. For example, the output of all the commands in the list can be redirected to a single stream.

Group commands in a sub-shell: ( )


Placing a list of commands between parentheses causes a subshell to be created, and each of the commands in list to be executed in that subshell, without removing non-exported variables.

Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

Group commands in the current shell: { }

  { list; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

In addition to the creation of a subshell, there is a subtle difference between these two constructs due to historical reasons. The braces are reserved words, so they must be separated from the list by blanks. The parentheses are operators, and are recognized as separate tokens by the shell even if they are not separated from the list by whitespace.

The exit status of both of these constructs is the exit status of list.

Return a binary result of expression: [[ ]]

   [[ expression ]] 

Return a status of 0 or 1 depending on the evaluation of the conditional expression. Word splitting and filename expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.

When the == and != operators are used, pattern matching will be done on the string to the right of the operator.
The return value is 0 if the string matches or does not match the pattern, respectively, and 1 otherwise.

Any part of the pattern can be quoted to force it to be matched as a string.

Expressions within the [[ ]] can be combined using the following operators, listed in decreasing order of precedence:

( expression )               Returns the value of expression.
                             This can be used to override the normal precedence of operators. 

! expression True if expression is false.
expression1 && expression2 True if both expression1 and expression2 are true.
expression1 || expression2 True if either expression1 or expression2 is true.

The && and || commands do not execute expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.

Arithmetic expansion

Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:

$(( expression ))

The expression is treated as if it were within double quotes, but a double quote inside the parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions can be nested.

If the expression is invalid, Bash prints a message indicating failure to the standard error and no substitution occurs.


Bash Syntax

Copyright © 1999-2017
Some rights reserved