9

I have a file named test.sh:

#!/bin/bash -o pipefail
echo "Running test"
git diff HEAD^ HEAD -M --summary |
grep delete | 
cut --delimiter=' ' -f 5

When I try to run this script as:

./test.sh

I get:

/bin/bash: line 0: /bin/bash: ./test: invalid option name

I ran cat -v test.sh to check if there are carriage returns or anything, but that doesn't seem to be the case. I can run the script if I just run it as bash test.sh. Grateful for any help, and lmk if I can provide more info!

  • 1
    You can pass a single argument in the shebang, and you're trying to pass it two (-o and pipefail). I don't know of any system where that (still) works. –  Aug 01 '19 at 17:45
  • If this is linux, check your execve man page. – glenn jackman Aug 01 '19 at 17:47

1 Answers1

9

The Linux kernel treats everything following the first “word” in the shebang line as a single argument. One solution is to set -o later in the script:

#!/bin/bash

set -o pipefail

echo "Running test" git diff HEAD^ HEAD -M --summary | grep delete | cut --delimiter=' ' -f 5

x-yuri
  • 3,373
Vinz
  • 2,150
  • 14
  • 16