Skip to content

Files

Latest commit

 

History

History
36 lines (24 loc) · 1.07 KB

SC2205.md

File metadata and controls

36 lines (24 loc) · 1.07 KB

Pattern: Binary operator as the first parameter in a subshell

Issue: -

Description

Tests like -eq to check numeric equality or \< for string comparison only work are actually parameters to the test command, and only work as tests in that context. [ is an alias for test, so you'll frequently see them written as [ 1 -eq 2 ].

( .. ) is completely unrelated, and is a subshell mostly used to scope shell modifications. They should not be used in if or while statements in shell scripts.

If you wanted to test a condition, rewrite the ( .. ) to [ .. ].

Example of incorrect code:

if ( 1 -lt 2 )
then
  echo "1 is less than 2"
fi

Example of correct code:

if [ 1 -lt 2 ]
then
  echo "1 is less than 2"
fi

Exceptions

This error is triggered by having a binary operator as the first parameter in a subshell, and could falsely trigger on e.g. if ( grep -eq "foo|bar" file ). In these cases, check whether the subshell is actually needed.

Further Reading