Skip to content

Bash Options

  • 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)
  • 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)

Option NameShort FlagEnable CommandDisable CommandBehavior
errexiteset -eset +e-e: Script aborts immediately if a command fails.
+e: Script ignores failures and continues (Default).
verbosevset -vset +vPrints shell input lines exactly as they are read by the interpreter.

Because -e is active, the script encounters the missing file, throws an error, and terminates before reaching the echo command.

exit_on_error.sh
#!/bin/bash
set -e
echo "Check non-existing file"
cat non-existing-file.txt
echo "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

Because +e is active (or implicitly default), the script logs the cat error but continues executing the remaining lines.

skip_error.sh
#!/bin/bash
# default value is +e
set +e
echo "Check non-existing file"
cat non-existing-file.txt
echo "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