kdubs
kdubs

Reputation: 1722

bash with multiple long options

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

Answers (1)

weibeld
weibeld

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:

  • All words as a single argument (your case)
  • Each word as a separate argument (what you were expecting)
  • Only first word as a single argument
  • No arguments at all

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

Related Questions