1090

I'm studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file.

The script has the following code:

#!/bin/bash
set -e
# Automatically added by dh_installinit
if [ "$1" = install ]; then
   if [ -d /usr/share/MyApplicationName ]; then
     echo "MyApplicationName is just installed"
     return 1
   fi
   rm -Rf $HOME/.config/nautilus-actions/nautilus-actions.conf
   rm -Rf $HOME/.local/share/file-manager/actions/*
fi
# End automatically added section

My first query is about the line:

set -e

I think that the rest of the script is pretty simple: It checks whether the Debian/Ubuntu package manager is executing an install operation. If it is, it checks whether my application has just been installed on the system. If it has, the script prints the message "MyApplicationName is just installed" and ends (return 1 mean that ends with an “error”, doesn’t it?).

If the user is asking the Debian/Ubuntu package system to install my package, the script also deletes two directories.

Is this right or am I missing something?

serghei
  • 3,069
  • 2
  • 30
  • 48
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 61
    [set -e](http://stackoverflow.com/questions/3474526/stop-on-first-error) – Anders Lindahl Oct 27 '13 at 19:07
  • 85
    reason why you couldn't find this in google: -e in your query is interpreted as negation. Try following query: bash set "-e" – Maleev Oct 31 '14 at 22:08
  • 4
    @twalberg When I've asked myself the same question, I was looking at `man set` – Sedat Kilinc Sep 03 '17 at 17:09
  • 11
    if you're looking how to turn it off, swap the dash to a plus prefix: `set +e` – Tom Saleeba Mar 13 '18 at 06:37
  • 2
    @twalberg but asking real people is so much more interesting than just making a request from a robot ;-). – vdegenne Apr 29 '18 at 02:45
  • @2GN The jury's still out... I'm not entirely convinced every questioner/commenter/answerer I've ever encountered on SO and family is "real people" and not a robot... ;-) – twalberg Apr 29 '18 at 03:13
  • It is explained here https://www.quora.com/What-is-the-difference-between-set-+e-and-set-e-in-a-bash-script – posix99 Jun 05 '18 at 13:36

10 Answers10

1110

From help set :

  -e  Exit immediately if a command exits with a non-zero status.

But it's considered bad practice by some (bash FAQ and irc freenode #bash FAQ authors). It's recommended to use:

trap 'do_something' ERR

to run do_something function when errors occur.

See http://mywiki.wooledge.org/BashFAQ/105

Jakov
  • 720
  • 1
  • 12
  • 29
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 22
    What would the do_something be if I wanted the same semantics as "Exit immediately if a command exits with a non-zero status"? – CMCDragonkai May 14 '14 at 02:37
  • 1
    when exiting in this way, you also get a non-zero status – Conan Oct 29 '14 at 15:08
  • 13
    The `ERR` trap is not inherited by shell functions, so if you have functions, `set -o errtrace` or `set -E` will allow you to just set the trap once and apply it globally. – ykay Jul 17 '15 at 18:21
  • @Conan, how about `trap 'exit 1' ERR`? I must admit, I'm not familiar with exactly when `ERR` fires. Is it like `set -e` or `set -o pipefail`? Or something else? – Aaron McDaid Dec 03 '15 at 21:26
  • @AaronMcDaid I'm afraid I no longer have access to the environment where I was doing this stuff (previous job), and I can't quite remember the setup. I was never an expert at this, maybe someone else knows more about `ERR`? – Conan Dec 08 '15 at 11:55
  • 39
    does `trap 'exit' ERR` do *anything* different from `set -e`? – Andy Jul 11 '16 at 20:58
  • 31
    if it's bad practice then why it's used in [Debian packages](http://unix.stackexchange.com/q/325705/44425)? – phuclv Dec 07 '16 at 06:35
  • 11
    It's *not* universally considered bad practice. As with many disfavored language construct, it has its place. The main problem with it is that the behavior in edge cases is somewhat non-intuitive. – William Pursell Dec 23 '16 at 21:26
  • 10
    I, too, disagree that it's considered bad practice. The link you provided mentions its not a complete solution and that there are gotchas, as is the case with any type of programming solution. There's a right time to use it and a wrong time to use it. As rking states in the article you linked to, "It has useful semantics, so to exclude it from the toolbox is to give into FUD." – GuyPaddock Jul 27 '17 at 17:41
  • 3
    @GuyPaddock, ...the usernames may not mean much to folks who haven't spent time in the freenode #bash channel, but the people on the "set -e shouldn't be used" side of that debate are known entities -- greycat wrote the BashFAQ in the first place; geirha wrote a bunch of tooling including the "evalbot" sandbox used for testing commands in isolated VMs. This is an argument-from-authority, sure, but as someone who spent a decade there, I have no idea who this "RKing" person is, but I surely *do* know the people they're disagreeing with. – Charles Duffy Dec 14 '18 at 22:02
  • 3
    ...and insofar as the people they're disagreeing with are folks with a lot of time/experience trying to provide support to users of bash in the real world (and thus learning about where and how new users get tripped up), the extent to which that experience informs their beliefs about which language features are best avoided is probably worth some attention. Similarly, I'd trust someone who's spent years helping debug JavaScript written by n00bs to that language to know which language features are error-prone and best avoided more than I would some random JS developer. – Charles Duffy Dec 14 '18 at 22:03
  • This can also be achieved adding `-e` to the shebang see https://unix.stackexchange.com/questions/15998/what-does-the-e-do-in-a-bash-shebang – lony Jun 18 '19 at 11:44
  • 1
    The answer is not details, it's not clear what neither 'do_something' nor ERR means. The provided link doesn't explain it at all. In fact, they mention `trap` only once in the question. An example would have more sense. – Onkeltem May 04 '22 at 09:19
197

set -e stops the execution of a script if a command or pipeline has an error - which is the opposite of the default shell behaviour, which is to ignore errors in scripts. Type help set in a terminal to see the documentation for this built-in command.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • 71
    It only stops execution if the **last** command in a pipeline has an error. There's a Bash specific option, `set -o pipefail` which can be used to propagate errors so that the return value of the pipeline command is non-zero if one of the preceding commands exited with a non-zero status. – Anthony Geoghegan Nov 09 '15 at 22:25
  • 6
    Keep in mind that `-o pipefail` means only that the *exit status* of the first non-zero (i.e. erroring in `-o errexit` terms) command of the pipeline is propagated to the end. The remaining commands in the pipeline *still run*, even with `set -o errexit`. For example: `echo success | cat - <(echo piping); echo continues`, where `echo success` represents a successful, but fallible command, will print `success`, `piping`, and `continues`, but `false | cat - <(echo piping); echo continues`, with `false` representing the command now erroring silently, will still print `piping` before exiting. – bb010g Aug 23 '19 at 07:26
108

I found this post while trying to figure out what the exit status was for a script that was aborted due to a set -e. The answer didn't appear obvious to me; hence this answer. Basically, set -e aborts the execution of a command (e.g. a shell script) and returns the exit status code of the command that failed (i.e. the inner script, not the outer script).

For example, suppose I have the shell script outer-test.sh:

#!/bin/sh
set -e
./inner-test.sh
exit 62;

The code for inner-test.sh is:

#!/bin/sh
exit 26;

When I run outer-script.sh from the command line, my outer script terminates with the exit code of the inner script:

$ ./outer-test.sh
$ echo $?
26
Reactive
  • 3
  • 2
entpnerd
  • 10,049
  • 8
  • 47
  • 68
73

As per bash - The Set Builtin manual, if -e/errexit is set, the shell exits immediately if a pipeline consisting of a single simple command, a list or a compound command returns a non-zero status.

By default, the exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (it's disabled by default).

If so, the pipeline's return status of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

If you'd like to execute something on exit, try defining trap, for example:

trap onexit EXIT

where onexit is your function to do something on exit, like below which is printing the simple stack trace:

onexit(){ while caller $((n++)); do :; done; }

There is similar option -E/errtrace which would trap on ERR instead, e.g.:

trap onerr ERR

Examples

Zero status example:

$ true; echo $?
0

Non-zero status example:

$ false; echo $?
1

Negating status examples:

$ ! false; echo $?
0
$ false || true; echo $?
0

Test with pipefail being disabled:

$ bash -c 'set +o pipefail -e; true | true | true; echo success'; echo $?
success
0
$ bash -c 'set +o pipefail -e; false | false | true; echo success'; echo $?
success
0
$ bash -c 'set +o pipefail -e; true | true | false; echo success'; echo $?
1

Test with pipefail being enabled:

$ bash -c 'set -o pipefail -e; true | false | true; echo success'; echo $?
1
Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
30

This is an old question, but none of the answers here discuss the use of set -e aka set -o errexit in Debian package handling scripts. The use of this option is mandatory in these scripts, per Debian policy; the intent is apparently to avoid any possibility of an unhandled error condition.

What this means in practice is that you have to understand under what conditions the commands you run could return an error, and handle each of those errors explicitly.

Common gotchas are e.g. diff (returns an error when there is a difference) and grep (returns an error when there is no match). You can avoid the errors with explicit handling:

diff this that ||
  echo "$0: there was a difference" >&2
grep cat food ||
  echo "$0: no cat in the food" >&2

(Notice also how we take care to include the current script's name in the message, and writing diagnostic messages to standard error instead of standard output.)

If no explicit handling is really necessary or useful, explicitly do nothing:

diff this that || true
grep cat food || :

(The use of the shell's : no-op command is slightly obscure, but fairly commonly seen.)

Just to reiterate,

something || other

is shorthand for

if something; then
    : nothing
else
    other
fi

i.e. we explicitly say other should be run if and only if something fails. The longhand if (and other shell flow control statements like while, until) is also a valid way to handle an error (indeed, if it weren't, shell scripts with set -e could never contain flow control statements!)

And also, just to be explicit, in the absence of a handler like this, set -e would cause the entire script to immediately fail with an error if diff found a difference, or if grep didn't find a match.

On the other hand, some commands don't produce an error exit status when you'd want them to. Commonly problematic commands are find (exit status does not reflect whether files were actually found) and sed (exit status won't reveal whether the script received any input or actually performed any commands successfully). A simple guard in some scenarios is to pipe to a command which does scream if there is no output:

find things | grep .
sed -e 's/o/me/' stuff | grep ^

It should be noted that the exit status of a pipeline is the exit status of the last command in that pipeline. So the above commands actually completely mask the status of find and sed, and only tell you whether grep finally succeeded.

(Bash, of course, has set -o pipefail; but Debian package scripts cannot use Bash features. The policy firmly dictates the use of POSIX sh for these scripts, though this was not always the case.)

In many situations, this is something to separately watch out for when coding defensively. Sometimes you have to e.g. go through a temporary file so you can see whether the command which produced that output finished successfully, even when idiom and convenience would otherwise direct you to use a shell pipeline.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 5
    this is an excellent answer. and it promotes the best practice. I had exactally the same problem from GREP command and i really did not want to remove the 'set -e' – soMuchToLearnAndShare Apr 27 '20 at 16:16
  • Of course, [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) doesn't work at all with `set -e`; but there are already many other reasons to avoid that antipattern. – tripleee May 04 '23 at 16:54
28

set -e The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful. In all widely used general-purpose programming languages, an unhandled runtime error - whether that's a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.

  • By default, bash does not do this. This default behavior is exactly what you want if you are using bash on the command line
  • you don't want a typo to log you out! But in a script, you really want the opposite.
  • If one line in a script fails, but the last line succeeds, the whole script has a successful exit code. That makes it very easy to miss the error.
  • Again, what you want when using bash as your command-line shell and using it in scripts are at odds here. Being intolerant of errors is a lot better in scripts, and that's what set -e gives you.

copied from : https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425

this may help you .

Rockinroll
  • 394
  • 6
  • 11
  • 2
    "This default behavior is exactly what you want if you are using bash on the command line" -- to be more clear: if you use `set -e` when running bash commands a simple typo could cause your bash session to just exit immediately. Try running a new terminal, `set -e`, then `lsd`. Bye goes terminal. – jcollum Dec 16 '21 at 20:05
25

I believe the intention is for the script in question to fail fast.

To test this yourself, simply type set -e at a bash prompt. Now, try running ls. You'll get a directory listing. Now, type lsd. That command is not recognized and will return an error code, and so your bash prompt will close (due to set -e).

Now, to understand this in the context of a 'script', use this simple script:

#!/bin/bash 
# set -e

lsd 

ls

If you run it as is, you'll get the directory listing from the ls on the last line. If you uncomment the set -e and run again, you won't see the directory listing as bash stops processing once it encounters the error from lsd.

  • Does this answer add any insight or information that wasn't already given in others on the question? – Charles Duffy Dec 14 '18 at 22:06
  • 13
    I think it offers a clear, succinct explanation of the functionality that is not present in the other answers. Nothing additional, just more focused than the other responses. – Kallin Nagelberg Dec 17 '18 at 16:06
  • @CharlesDuffy I think it does. It is much more useful than just saying "look at the man page" – KansaiRobot Apr 26 '21 at 10:59
  • The other answer _doesn't_ just say "look at the man page" -- it pulls out the specific part of the man page that's relevant and important. It's the failure to be specific (and the requirement that the reader then do their own research) that makes "look at the man page" unhelpful. – Charles Duffy Apr 26 '21 at 14:02
  • I also think this answer is helpful @CharlesDuffy – jcollum Dec 16 '21 at 20:04
14
Script 1: without setting -e
#!/bin/bash
decho "hi"
echo "hello"
This will throw error in decho and program continuous to next line

Script 2: With setting -e
#!/bin/bash
set -e
decho "hi" 
echo "hello"
# Up to decho "hi" shell will process and program exit, it will not proceed further
Manikandan Raj
  • 151
  • 1
  • 3
6

It stops execution of a script if a command fails.

A notable exception is an if statement. eg:

set -e
false
echo never executed
set -e
if false; then
  echo never executed
fi

echo executed

false

echo never executed
mxcl
  • 26,392
  • 12
  • 99
  • 98
2
cat a.sh
#! /bin/bash

#going forward report subshell or command exit value if errors
#set -e
(cat b.txt)
echo "hi"

./a.sh; echo $?
cat: b.txt: No such file or directory
hi
0

with set -e commented out we see that echo "hi" exit status being reported and hi is printed.

cat a.sh
#! /bin/bash

#going forward report subshell or command exit value if errors
set -e
(cat b.txt)
echo "hi"

./a.sh; echo $?
cat: b.txt: No such file or directory
1

Now we see b.txt error being reported instead and no hi printed.

So default behaviour of shell script is to ignore command errors and continue processing and report exit status of last command. If you want to exit on error and report its status we can use -e option.

Miten
  • 356
  • 7
  • 23