Reputation: 1722
I've got a bash script that I don't want reading the bashrc or bash profile. so I tried this:
#!/bin/bash --norc --noprofile
echo ran
it gives me this error:
> ./tst.sh
/bin/bash: --norc --noprofile: invalid option
Usage: /bin/bash [GNU long option] [option] ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--rpm-requires
--restricted
--verbose
--version
--wordexp
it appears that I may only specify one long option, but I can't find that documented. the --noprofile is probably over kill, but I'm still curious why I can't specify multiple long options
Upvotes: 2
Views: 1307
Reputation: 15282
The point is that you can specify at most one word of options on the shebang line (it doesn't have anything to do with long or short options).
More precisely, the execve
function of your OS passes everything that follows #!/bin/bash
as a single argument to bash. This results in the same as the execution of
bash "--norc --noprofile"
which produces your error.
As pointed out by the links in the comments (in-ulm.de/~mascheck/various/shebang/#splitting and manpages.ubuntu.com/manpages/trusty/en/man2/execve.2.html) this behaviour is OS-specific, and the variations include:
Conclusion: on your OS you can't specify multiple words of arguments for bash on the shebang line, and even if you could, you probably shouldn't, because the script would fail on operating systems that don't support multiple shebang arguments.
Upvotes: 2