Debugging
Debugging helps identify errors in shell scripts.
-
Syntax check:
bash -n script.sh -
Trace execution:
bash -x script.sh(shows commands and expansions) -
Verbose:
bash -v script.sh(prints lines as they are read) -
Set options:
set -eexit on errorset -uexit on undefined variableset -o pipefailcatch errors in pipelines
Input:
set -x
name="Pandu"
echo "User: $name"
Output:
+ name='Pandu'
+ echo 'User: Pandu'
User: Pandu
Explanation: set -x prints each command before execution, showing variable expansions.
References: