Score:3

What does "set -eu" do in a bash script?

cn flag

I have googled and could not find an explanation of this combination.

What does set -eu do in the start of a bash shell script?

(I could find some explanations for -e but not -eu.)

David avatar
cn flag
Look up the bash set command and the switches -e and -u one at a time.
U. Windl avatar
gf flag
Would the question be answered when saying option `-eu`is the same as option `-e`and option `-u`? Also I think you should have tried a bit harder to answer it before asking this question.
in flag
@user253751 there's also `+eu` to revert `-eu`, things get messy :D
cn flag
@U.Windl Searched for `-u` . Could not find anything. Therefore I asked
conny avatar
gf flag
These are SHELL OPTIONS, toggled by using "set" or "shopt". #teachamantosearch :-)
Score:7
pl flag

In the bash man page under the section for shell builtin commands the set command is explained. -e and -u can be set separately, but are often combined when debugging along with -x to set -eux or in the shebang to #!/bin/bash -eux

-e      Exit  immediately  if  a  pipeline  (which  may  consist of a single simple
        command), a list, or a compound command (see SHELL  GRAMMAR  above),  exits
        with  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  following  the  if or elif reserved words, 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 value is being inverted with !.  If a compound command other than  a
        subshell  returns  a  non-zero status because a command failed while -e was
        being ignored, the shell does not exit.  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
        above),  and  may cause subshells to exit before executing all the commands
        in the subshell.

-u      Treat  unset variables and parameters other than the special parameters "@"
        and "*" as an error when performing parameter expansion.  If  expansion  is
        attempted  on  an  unset  variable  or parameter, the shell prints an error
        message, and, if not interactive, exits with a non-zero status.
in flag
To add to the excellent answer, you would see this in scripts to improve error handling. Without `-e` the script will continue its execution when an underlying command returned an error code. In many cases, you'd want to terminate the script at that point. `-u` protects against accidental usage of a variable that is not defined, for example, if the script uses a variable `$foo`, but its value wasn't set before, the `-u` flag will tell bash to stop execution with an error.
cn flag
@oleksii Thanks for your excellent addition!.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.