Bash Options
Shell Options Summary
Section titled “Shell Options Summary”- Purpose: Options modify the execution behavior of a shell or script.
- Enable Options: Use the (minus) sign.
- Long form:
set -o <option-name>(e.g.,set -o verbose) - Short form:
set -<abbrev>(e.g.,set -v)
- Long form:
- Disable Options: Use the
+(plus) sign.- Long form:
set +o <option-name>(e.g.,set +o verbose) - Short form:
set +<abbrev>(e.g.,set +v)
- Long form:
Flag Behavior Table
Section titled “Flag Behavior Table”| Option Name | Short Flag | Enable Command | Disable Command | Behavior |
|---|---|---|---|---|
| errexit | e | set -e | set +e | -e: Script aborts immediately if a command fails. |
+e: Script ignores failures and continues (Default). | ||||
| verbose | v | set -v | set +v | Prints shell input lines exactly as they are read by the interpreter. |
Example 1: set -e (Exit on Error)
Section titled “Example 1: set -e (Exit on Error)”Because -e is active, the script encounters the missing file, throws an error, and terminates before reaching the echo command.
#!/bin/bashset -e
echo "Check non-existing file"cat non-existing-file.txtecho "moving on" # This line will NEVER execute.Output:
$ ./exit_on_error.sh Check non-existing file cat: non-existing-file.txt: No such file or directory
Example 2: set +e (Skip Error / Default)
Section titled “Example 2: set +e (Skip Error / Default)”Because +e is active (or implicitly default), the script logs the cat error but continues executing the remaining lines.
#!/bin/bash# default value is +eset +e
echo "Check non-existing file"cat non-existing-file.txtecho "moving on" # This line executes normally.Output:
$ ./skip_error.sh Check non-existing file cat: non-existing-file.txt: No such file or directory moving on