Search code examples
linuxbashshellsh

What does 'set -e' mean in a Bash script?


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 means that the script exits 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?


Solution

  • From help set and Bash Reference Documentation: The Set Builtin:

      -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 Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected?