2

I am playing with a docker CentOS image, and find executing "/usr/bin/env bash -x" command is OK in terminal:

bash-4.1# /usr/bin/env bash -x
bash-4.1# exit
+ exit
exit

But after writing this command into a script and execute it, it doesn't work, and prompts "No such file or directory":

bash-4.1# ls -lt a.sh
-rwxr-xr-x. 1 root root 23 May 20 04:27 a.sh
bash-4.1# cat a.sh
#!/usr/bin/env bash -x
bash-4.1# ./a.sh
/usr/bin/env: bash -x: No such file or directory

Is there any difference between two methods?

Nan Xiao
  • 15,571
  • 16
  • 87
  • 149
  • 1
    No there is no difference, make sure the file content is what you think it is (check with `od -c`, I suspect you've got some funny character instead of a space) and that bash is in the path. – n. 1.8e9-where's-my-share m. May 20 '15 at 08:38
  • Making `set -x` the first line of the script avoids the need to pass arguments in the hashbang. – chepner May 20 '15 at 13:25

2 Answers2

5

The short answer is that you only get one parameter to the interpreter which is specified via the "#!" mechanism. That became "bash -x".

Usually the limitation is more apparent, e.g., using

#!/bin/bash -x -i

would pass "-x -i" as the parameter, and get unexpected results.

Sven Mascheck comments on this in his page on the topic:

most systems deliver all arguments as a single string

Thomas Dickey
  • 47,166
  • 7
  • 60
  • 95
1

The shebang line should have at most one argument.
When you give more arguments, they will not be split. You can compare this with the commandline command

bash-4.1# /usr/bin/env "bash -x"
Walter A
  • 17,923
  • 2
  • 22
  • 40
  • 1
    Sheebangs support only one parameter in some operating systems (see [this answer](https://stackoverflow.com/a/4304187/297586)). In linux, this yields `/usr/bin/env: ‘"bash -x"’: No such file or directory`. One alternative in this case is to use `set -x` just below the sheebang. – nandilugio Apr 24 '18 at 14:40