Skip to content
Vidar Holen edited this page Aug 30, 2021 · 3 revisions

Remove superfluous (..) around test command to avoid subshell overhead.

Problematic code:

([ "$x" -gt 0 ]) && foo

Correct code:

[ "$x" -gt 0 ] && foo

Rationale:

You are wrapping a single test command in (..), creating an unnecessary subshell. This serves no purpose, but is significantly slower:

$ i=0; time while ( [ "$i" -lt 10000 ] ); do i=$((i+1)); done
real    0m6.998s
user    0m3.453s
sys     0m3.464s

$ i=0; time while [ "$i" -lt 10000 ]; do i=$((i+1)); done
real    0m0.055s
user    0m0.054s
sys     0m0.001s

Just delete the surrounding (..) since they serve no purpose and only slows the script down.

Exceptions:

This issue only affects performance, not correctness, and can be ignored for stylistic reasons.

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally