Skip to content

Instantly share code, notes, and snippets.

@rjchicago
Created August 29, 2024 18:39
Show Gist options
  • Save rjchicago/a0aa3210c65c162b672d7b4c58cc829a to your computer and use it in GitHub Desktop.
Save rjchicago/a0aa3210c65c162b672d7b4c58cc829a to your computer and use it in GitHub Desktop.
Bash: set -Eeou pipefail explained by Amazon Q

The code snippet you provided is:

set -Eeou pipefail

This is a Bash shell command that sets various options to control how the shell behaves and handles errors. Let's break down what each part of this command does:

set is a built-in Bash command used to set or unset shell options and positional parameters.

-E is an option that causes inherited traps to be inherited by shell functions, command substitutions, and commands executed in the environment where the trap was originally set.

-e is an option that causes the shell to exit immediately if any command exits with a non-zero status (i.e., if any command fails).

-o is an option that allows you to set or unset various shell options.

-u is a shell option that causes the shell to treat unset variables as an error and exit immediately when they are encountered.

pipefail is a shell option that causes the return value of a pipeline (a sequence of commands separated by the | character) to be the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully.

When combined, the command set -Eeou pipefail sets the following behavior for the shell:

  • Inherited traps are inherited by shell functions, command substitutions, and commands executed in the environment where the trap was originally set.
  • The shell will exit immediately if any command exits with a non-zero status (fails).
  • Unset variables are treated as an error, and the shell will exit immediately when they are encountered.
  • The return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully.

This combination of options is often used in shell scripts to ensure that the script exits immediately if any command fails or if an unset variable is encountered. It also ensures that the exit status of a pipeline accurately reflects the success or failure of the commands within the pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment