Debugging scripts, what is the difference between -x to set -euxo pipefail?

The main way I know to debug scripts is adding -x to the shabang (#!/bin/bash -x).

I recently came across a new way, adding set -euxo pipefail right under the shabang, as in:

#!/bin/bash
set -euxo pipefail

What is the main difference between the two ways of debugging? Are there times you would prefer one above the other?

As a freshman, after reading here, I couldn’t extract such conclusion.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First, I’m afraid that explanation of -o option served by http://explainshell.com is not entirely correct.

Given that set is a bulit-in
command, we can see its documentation with help by executing help set:

  -o option-name
      Set the variable corresponding to option-name:
          allexport    same as -a
          braceexpand  same as -B
          emacs        use an emacs-style line editing interface
          errexit      same as -e
          errtrace     same as -E
          functrace    same as -T
          hashall      same as -h
          histexpand   same as -H
          history      enable command history
          ignoreeof    the shell will not exit upon reading EOF
          interactive-comments
                       allow comments to appear in interactive commands
          keyword      same as -k
          monitor      same as -m
          noclobber    same as -C
          noexec       same as -n
          noglob       same as -f
          nolog        currently accepted but ignored
          notify       same as -b
          nounset      same as -u
          onecmd       same as -t
          physical     same as -P
          pipefail     the return value of a pipeline is the status of
                       the last command to exit with a non-zero status,
                       or zero if no command exited with a non-zero status
          posix        change the behavior of bash where the default
                       operation differs from the Posix standard to
                       match the standard
          privileged   same as -p
          verbose      same as -v
          vi           use a vi-style line editing interface
          xtrace       same as -x

As you can see -o pipefail means:

the return value of a pipeline is the status of the last command to
exit with a non-zero status, or zero if no command exited with a
non-zero status

But it doesn’t say: Write the current settings of the options to standard output in an unspecified format.

Now, -x is used for debugging as you already know it and -e will
stop executing after the first error in the script. Consider a script
like this:

#!/usr/bin/env bash

set -euxo pipefail
echo hi
non-existent-command
echo bye

The echo bye line will never be executed when -e is used because
non-existent-command does not return 0:

+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found

Without -e the last line would be printed because even though an
error happened we didn’t tell Bash to automatically exit:

+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found
+ echo bye
bye

set -e is often put at the top of the script to make sure that the
script will be stopped when the first error is encountered – for
example, if downloading a file failed it doesn’t make sense to extract
it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x