34

The below mentioned line of code used to work for me all the time on a Ubuntu 16.04 distribution, but suddenly option-name pipefail is an illegal option:

set -eu -o pipefail

returns:

set: Illegal option -o pipefail

Why does this happen? I run the command on a completely new installed system and as part of a shell script. The code is placed right at the beginning:

myscript.sh:

1 #!/bin/bash
2 set -eu -o pipefail
3 ...

The script is run as sudo:

sudo sh ./myscript.sh
1

2 Answers 2

47

You are running bin/sh, on Ubuntu it is a symbolic link pointing to /bin/dash, but pipefail is a bashism.

Make the script executable:

chmod +x myscript.sh

and then run the script as follows:

sudo ./myscript.sh
2
  • 2
    This works because the script starts with shebang #!/bin/bash which the kernel honors when executing it directly. sudo bash myscript.sh would also work, but like the previous situation it requires the callers of the script to know in which dialect exactly it's written; relying on shebang is great because script is self-documenting and you're free to rewrite it later to different shells (or python etc. or even native executable) 👍 Commented Oct 18, 2023 at 11:37
  • 2
    pipefail is not unique to bash, supported by multiple shells but not by dash. But focusing on bash is good, e.g. it lets you print friendlier errors relying on other bash features: trap 's=$? p=("${PIPESTATUS[@]}"); echo "$0: Error ${p[*]} on line $LINENO: $BASH_COMMAND"; exit $s' ERR; set -u -o pipefail Commented Oct 18, 2023 at 11:56
15

I had the same error when running script from zsh and the script began with incorrect shebang.

WRONG, missing ! after #:

#/bin/bash
rest-of-the-script

Correct:

#!/bin/bash
rest-of-the-script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.