Skip to content

Files

Latest commit

 

History

History
39 lines (25 loc) · 1022 Bytes

SC2204.md

File metadata and controls

39 lines (25 loc) · 1022 Bytes

Pattern: Unary test operator as the first command name in a subshell

Issue: -

Description

Tests like -d to see if something is a directory or -z to see if it's non-empty are actually flags 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 [ -d somedir ].

( .. ) 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 ( -d somedir )
then
  echo "It's a directory"
fi

Example of correct code:

if [ -d somedir ]
then
  echo "It's a directory"
fi

Exceptions

None.

This error is triggered by having a unary test operator as the first command name in a subshell, which won't normally happen.

Further Reading