17

What does the eu mean after #!/bin/bash -eu at the top of a bash script?

Normally I begin my bash scripts with this hashbang/shebang:

#!/bin/bash

but I just came across one with

#!/bin/bash -eu

and I have no idea why there is a -eu there. Reading the man bash pages doesn't seem to help me, but maybe I'm overlooking something.


Not a duplicate:

This is not a duplicate of Correct behavior of EXIT and ERR traps when using `set -eu`.

Quoting @ilkkachu in the comments below this question, directly addressing this:

...how -e and -u work with regard to traps or anything else is completely unrelated to how they and other single-character options can be given on the command line.

I agree with that. These are separate questions and answers with differing motivations behind them. That question is so different I would never even think to click on it by looking at its title OR its description when trying to understand the answer to my own question here, and the answers are vastly different too.

8
  • 1
    This will prove helpful: unix.stackexchange.com/q/15998
    – Quasímodo
    Commented Jun 26, 2020 at 17:56
  • 1
    Does this answer your question? Correct behavior of EXIT and ERR traps when using `set -eu` Commented Jun 26, 2020 at 20:03
  • @ThomasDickey, despite your 60,000 reputation, that seems a pretty far stretch to downvote this question and try to mark it as a duplicate of that completely unrelated question which happens to also use the -eu hashbang options. So no, it does not answer my question. Commented Jun 26, 2020 at 20:05
  • Correction: change "completely unrelated" --> "adjacently related, but not at all the same". I've added it as a "related" link to the bottom of my question now. Commented Jun 26, 2020 at 20:26
  • 1
    @ThomasDickey, how -e and -u work with regard to traps or anything else is completely unrelated to how they and other single-character options can be given on the command line.
    – ilkkachu
    Commented Jun 26, 2020 at 21:37

2 Answers 2

26

They're the same options as -e and -u to the set builtin. They can be given on the shell command line too, and they get given as command line arguments from the hashbang line too. (But note e.g. issues with Multiple arguments in shebang)

The online manual says, under "Invoking Bash", that

All of the single-character options used with the set builtin can be used as options when the shell is invoked.

The single character options are also explicitly listed in the invocation synopsis on the online manual (bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option] ... ), though not in the manpage.

set -u tells the shell to treat expanding an unset parameter an error, which helps to catch e.g. typos in variable names.

set -e tells the shell to exit if a command exits with an error (except if the exit value is tested in some other way). That can be used in some cases to abort the script on error, without explicitly testing the status of each and every command.

6
  • I found them on the manpage (on Debian).
    – Quasímodo
    Commented Jun 26, 2020 at 18:08
  • @Quasímodo, mine only has SYNOPSIS: bash [options] [command_string | file] and they're not explicitly listed under OPTIONS, there's just the mention that the options to set can be used. The online manual has them listed in the invocation synopsis along with every other option: bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option] [-O shopt_option] -c string [argument …]
    – ilkkachu
    Commented Jun 26, 2020 at 18:11
  • Oh, that's true, mine does not have it explicitly under OPTIONS either.
    – Quasímodo
    Commented Jun 26, 2020 at 18:13
  • Looks like one would have to know to check the set pages after reading the start of the man bash page, but man set doesn't show these options either, so you'd have to know to then check help set, which does show the -e and -u options, and their brief descriptions. That logical flow process to find this information is quite obscure. Commented Jun 26, 2020 at 19:48
  • 1
    @GabrielStaples, well, yeah. The shell builtins don't have man pages of their own. set is described in Bash's manpage under "Shell builtin commands", but sure, you need to know it's a function of the shell and not an external program. (It has to be internal to the shell since it modifies the behavior of the shell itself, an external program can't well do that.)
    – ilkkachu
    Commented Jun 26, 2020 at 19:58
8

Those are options for the Bash script as described in man bash, set builtin command.

Right at the beginning of man bash:

OPTIONS

All of the single-character shell options documented in the description of the set builtin command, including -o, can be used as options when the shell is invoked.

Now, set is not a separate command, it is a shell builtin. That is why you found no manpage with man set. Instead, down in the bash manual you will find SHELL BUILTIN COMMANDS section. There will be the set builtin, where this is to be found:

-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. (...)

-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.

So, in my example below, the -u flag will make the script exit on the first echo, since notDeclared was not declared. Similarly, it would exit on the grep line because of the -e flag, since nonExistentFile does not exist and grep, a simple command, exits with error (non-zero) status.

#!/bin/bash -ue

echo "$notDeclared"
grep something nonExistentFile
echo "I am never going to be echoed..."

If you want to see it exiting on grep, then either remove the -u or move grep to the top.


Note on grep: grep exits with non-zero status if the pattern is not found in the file. So, in the sample above, if you created nonExistentFile without the string something in its contents, grep would exit with 1 (non-zero) and therefore, because of the -e flag, the execution would be terminated. That may be surprising, as not finding an expression in a file is not really an "error".

2
  • My man bash doesn't show any of this information. Neither does man set. help set shows very brief descriptions of it, however, but not the long descriptions you have quoted above. Note that I'm on Linux Ubuntu 18.04. Are you on Mac? Commented Jun 26, 2020 at 19:51
  • 1
    @GabrielStaples Please see edit. That is a common mistake. Some commands are builtin and thus don't have a standalone manpage.
    – Quasímodo
    Commented Jun 26, 2020 at 19:59

You must log in to answer this question.

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