Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between "bash -eu" . and "bash -e"

Apologies if this is a repost. I did search (no luck) before posting this.

"bash -e" will error/fail if there is any error. Doesn't it include the "bash -u" condition? If a parameter is not set won't a command using that parameter fail and caught by "bash -e" ?

Isn't "bash -eu" equal to "bash -e" in that case?

like image 235
uucp007 Avatar asked May 16 '18 19:05

uucp007


People also ask

What is the difference between -Bash and Bash?

Now what are difference between -bash and bash? Bash started with a dash - as the first character for the command name (argument #0) starts as a login shell. (So does a shell given the -l / --login switch.) That affects the startup files it reads.

What is the difference between if –E and if –s in Bash?

The “if –e” and “if –s” are such operators in Bash, used for testing the existence of a file. The difference between the two is that the former only tests the existence of a file, whereas the latter also checks if there are any contents in that file or not.

What is the difference between-E and-F in Bash?

$ man bash -e file True if file exists. -f file True if file exists and is a regular file. A regular file is something that isn't a directory, symlink, socket, device, etc. Is this answer outdated? -e checks for any type of filesystem object; -f only checks for a regular file.

What is set +E in bash script?

(more)Loading…. In bash, set +e is basically the default: If you get an error in a bash script, it normally barfs out an error to the system (this is called an exit code) but the script will continue running.


1 Answers

No, bash -e (bash started with the errexit shell option set) is not the same as bash -e -u (bash started with both errexit and nounset set).

Example:

$ bash -e -c 'echo "hello $string"'
hello
$ echo "$?"
0
$ bash -e -u -c 'echo "hello $string"'
bash: string: unbound variable
$ echo "$?"
1

Using an unset variable under only errexit is not an error, it just expands to an empty string.

Also:

$ bash -u -c 'echo "hello $string"'
bash: string: unbound variable
$ echo "$?"
127

This shows a subtle difference between -e and -u. With only -u, bash exits with code 127, which translates into a "command not found" error. With both -e and -u, bash exits with a more generic error code of 1.


These things holds true for the POSIX sh shell as well, although I don't believe that the 127 exit status is explicitly required for the last example.

like image 179
Kusalananda Avatar answered Oct 19 '22 10:10

Kusalananda