DevOps · Flashcard

Why is [[ ... ]] generally preferred over [ ... ] in Bash scripts?

  • A[[ is a shell keyword that avoids word-splitting and globbing of unquoted variables
  • B[[ is an external program that runs measurably faster than the builtin [ command
  • C[[ automatically converts its string operands to numbers before comparing them
  • D[[ is the only construct able to test whether a file exists on the disk

Why this is the answer

[[ ]] is a Bash keyword, so the shell parses it specially: unquoted variables are not word-split or glob-expanded, and it adds ==/=~ pattern matching. Option two is backwards — [ is the command-like builtin and neither is external. Option three is false — comparison stays string unless you use -eq. Option four is false — both forms support -f/-d.

Official docs
Study in Gnoseed →