Skip to content

Files

Latest commit

 

History

History
48 lines (30 loc) · 1.56 KB

SC1088.md

File metadata and controls

48 lines (30 loc) · 1.56 KB

Pattern: Incorrect use of Bash parentheses

Issue: -

Description

Parentheses are shell syntax and must be used correctly.

For commands that expect literal parentheses, such as grep or find, the parentheses need to be quoted or escaped so the shell does not interpret them, but instead passes them to the command.

For shell syntax, the shell does not use them the way most other languages do, so avoid guessing at syntax based on previous experience. In particular:

  • Parentheses are NOT used to call functions.

  • Parentheses are NOT used to group expressions, except in arithmetic contexts.

  • Parentheses are NOT used in conditional statements or loops.

  • Parentheses are used differently in different contexts. ( .. ), $( .. ), $(( .. )) and var=(..) are completely separate and independent structures with different meanings, and can not be broken down into operations on expressions in parentheses.

    In C-like languages, ++ can't be broken down into two + operations, so you can't e.g. use + + or +(+). In the same way, all of the above are completely unrelated so that you can't do $(1+1) or $( (1+1) ) in place of $(( 1+1 )).

If you are trying to use parentheses for shell syntax, look up the actual syntax of the statement you are trying to use.

Example of incorrect code:

grep ^(.*)\1$ file

or

var = somefunction(value)

Example of correct code:

grep '^(.*)\1$' file

or

var=$(somefunction value)

Further Reading