32

I have a script that I found and I am trying to get it to run, it just downloads and install Apache runtime libraries. The first line is set -e and it is causing an error; what does set -e do?

2 Answers 2

26

This builtin is so complicated that it deserves its own section.

set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables.

The -e option

-e

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a subshell command enclosed in parentheses (see Command Grouping), or one of the commands executed as part of a command list enclosed by braces (see Command Grouping) returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command’s return status is being inverted with !. A trap on ERR, if set, is executed before the shell exits.

This option applies to the shell environment and each subshell environment separately (see Command Execution Environment), and may cause subshells to exit before executing all the commands in the subshell.

Source: www.gnu.org

Edited due to @psusi's comment below.

Additionally you can read the bash's manual page

man bash 

at the section: SHELL BUILTIN COMMANDS

or issue

help set 

for a short help message.

2
  • 1
    Since it is a builtin command, you want help set and the full bash manual is info bash, if you have bash-doc installed.
    – psusi
    Sep 18, 2013 at 2:17
  • Thanks for the clarification @psusi. help set and/or man bash are the correct ones for set .
    – NickTux
    Sep 18, 2013 at 6:19
17

set -e in short, when it is at the top of your bash script, tells script to exit as soon as any line in the script fails (with some exceptions listed in the manual.)[1]

As a debugging option, it's often used with set -x, which is to print each expanded command line before execution with a "+" sign.[2]

See more at:

[1] https://www.peterbe.com/plog/set-ex

[2] http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html

1
  • 2
    Thanks for simple explanation. I often see set -xe in Dockerfiles and now I know the reason :)
    – ymakux
    Oct 21, 2017 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .