Bash Options with Set: Examples and reference

Bash Options with Set: Examples and reference

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Echo commands to stdout

Print every command you run to the terminal (standard output) using set -x

#!/usr/bin/env bash

set -x

# other commands below

Abort if any command fails

This prevents next commands from running in a bad state

Use set -e and set -o pipefail to make the full script abort and exit if any single command fails.

#!/usr/bin/env bash

set -e
set -o pipefail

command1

# command2 will not be executed if command 1 fails
command2

Set multiple options

Use set -ex to set both options, e and x at the same time:

#!/usr/bin/env bash

set -ex

# both x and e options have been set