Bash
Bash is the GNU Project's shell—the Bourne Again SHell. This is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.1
Examples
Get Script Location1
Comparisons
Comparisons
Single-bracket [ ... ] (the POSIX test) and double-bracket [[ ... ]] (the Bash conditional) look similar but behave differently. Use [ ... ] for portability; prefer [[ ... ]] in Bash scripts for safer and more powerful conditionals.
-
[]is the traditional POSIX test (often a shell builtin). It is portable to/bin/sh, requires careful quoting of expansions to avoid word-splitting and pathname expansion, and supports standard file- and string-test operators (e.g.-z,-n,-f,-eq,=/!=). Operators like<and>must be quoted or escaped because the shell may treat them as redirections. -
[[]]is a Bash (and ksh) keyword that provides extended conditional features: no word-splitting or pathname expansion on unquoted expansions (so variable tests are safer without always needing quotes), pattern matching with==(glob-style) and=~(regular-expression), and the ability to use&&/||and parentheses inside the conditional. It is not POSIX portable, so avoid it when you need a script to run under plain/bin/sh. - Conditional Expressions
Check for Environment Variable
Check Script Arguments
if [[ "$#" -eq 0 ]]; then
echo "No arguments provided."
exit 1
else
echo "The script received the following arguments:"
for arg in "$@"; do
echo "- $arg"
done
fi