The traditional [
command (also known as test
) is an external command that treats its arguments as separate words, making
it vulnerable to word splitting and pathname expansion issues. This can lead to unexpected behavior when variables contain spaces, special characters,
or when filenames match glob patterns.
The modern [[
construct is part of Bash syntax rather than a separate command. This means:
- Word splitting and pathname expansion do not occur within
[[ ]]
, making it more robust when handling variables that might contain
spaces or special characters
- It provides better error handling and more predictable behavior
- It offers additional features like regular expression matching with the
=~
operator
- It’s generally faster since it doesn’t require spawning an external process
Using [
can cause subtle bugs that are difficult to debug, especially when dealing with user input, filenames, or variables that might
be empty or contain unexpected characters.
What is the potential impact?
Using the traditional [
command instead of [[
can lead to script failures or unexpected behavior when:
- Variables contain spaces, tabs, or newlines
- Filenames contain special characters or match glob patterns
- Variables are empty or unset
These issues can cause scripts to behave unpredictably in production environments, potentially leading to data loss, security vulnerabilities, or
system instability.