15

I've been trying to run on Ubuntu 16 a bash script that I developed on CentOS 7.

The first line of the script is:

set -o nounset -o pipefail -o errexit

When I try to run this script, I get the following error:

project.sh: 6: set: Illegal option -o pipefail

How to solve this issue? I also the solution explain in the answer to this question but it did not help (my file is not a make).

4 Answers4

19

On Ubuntu the default shell is dash (aka Debian Almquist Shell), to which /bin/sh is symlink. When your shell script is run with #!/bin/sh, you are effectively trying to run it with the default shell. However, dash doesn't have the pipefail option, which is why you're getting the error.

# Verifying what /bin/sh is symlinked to dash
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 2月  17  2016 /bin/sh -> dash
# Verify that pipefail doesn't exist as option for dash
$ dash    
$ set -o | grep pipefail                                              
$ set -o pipefail
dash: 1: set: Illegal option -o pipefail
$ sh
$ set -o pipefail
sh: 1: set: Illegal option -o pipefail
# Try this same option in bash
$ bash --posix
bash-4.3$ set -o pipefail
bash-4.3$  
# no error
1

One more option that can cause this (or similar) issue: Windows line endings (CRLF, "\r\n") in script file.

Much more likely to occur on WSL than raw Ubuntu, but took me a while to find out.

Since line endings are wrong, interpreter reads the last argument exactly as pipefail\r, which is interpreted as unknown one. But \r is not visible in terminal, what makes hard to understand the cause.

The fix is to ensure that line endings in your text editor are set to LF.

R2RT
  • 111
1

try to use below flag then it work. I have validated it.

#!/bin/bash
set -e -o pipefail

to reset use

set +e +o pipefail

0

For those looking for a simple script that circumvents the dash issue with Ubuntu, you can use

bash | set +o pipefail