14

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

  • 3
    What is the shebang line in the script ? #!/bin/sh ? – Sergiy Kolodyazhnyy Feb 23 '17 at 15:08
  • 1
    Are you sure that the script is run by Bash? Do you invoke it with bash scriptname, it the first line #! /bin/bash? – AlexP Feb 23 '17 at 15:09
  • 2
    The first line is #!/bin/sh, but I was invoking it as sh project.sh as in CentOS! Now I tried to run bash project.sh and the problem disappeared! Thanks guys! – DavideChicco.it Feb 23 '17 at 15:11
  • @DavideChicco.it see my answer. sh is symlink to dash shell on Ubuntu. Also, start changing your habits. Use chmod +x script.sh and ./script.sh to run it. The way you do it sh ./script.sh has whole lot of issues of its own and easy to mess up – Sergiy Kolodyazhnyy Feb 23 '17 at 15:12
  • If the command bash project.sh fixed your error, then you should change your shebang line to #!/bin/bash that way you don't have to type in the bash in front of your script name. – Terrance Feb 23 '17 at 15:19

4 Answers4

18

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
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 2
    Just the command set -o will list all of the options supported by set. – Terrance Feb 23 '17 at 15:23
  • @Terrance I know, I just wanted to reproduce the error specifically. Although, I'll add that one as well. – Sergiy Kolodyazhnyy Feb 23 '17 at 15:28
  • sudo dpkg-reconfigure dash then No to unset dash as default shell. – Tuan Sep 07 '17 at 04:45
  • @Tuan I wouldn't do that. Although bash is made POSIX compliant and in theory everything should work just fine, there's a possibility that some system scripts might not work. Low but still a possibility – Sergiy Kolodyazhnyy Sep 17 '17 at 18:27
  • Thanks @SergiyKolodyazhnyy, just commented because I had the same issue and this command fixed mine. – Tuan Sep 18 '17 at 02:41
  • @SergiyKolodyazhnyy This makes a lot of sense (in my situation). How do I "fix" a script that has the following line in it at the start (for usage in Ubuntu, as I believe the script was originally written for/in CentOS) and the instructions are to run the script with ./script [#!/bin/bash set -e -o pipefail] – Andor Kiss Feb 16 '21 at 16:40
0

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

bash | set +o pipefail
Alec Gerona
  • 101
  • 2
0

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
  • 101
0

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