我有一个bash脚本,我不想阅读bashrc或bash个人资料。所以我试过了:
#!/bin/bash --norc --noprofile
echo ran
它给了我这个错误:
> ./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
似乎我只能指定一个长选项,但我无法找到记录。 --noprofile可能过度杀戮,但我仍然很好奇为什么我不能指定多个长期选项
答案 0 :(得分:2)
关键是你可以在shebang线上最多指定 一个 选项字(它与长或短选项没有任何关系)。
更确切地说,您的操作系统的execve
功能会将#!/bin/bash
后面的所有内容作为 单个 参数传递给bash。这导致与执行
bash "--norc --noprofile"
会产生错误。
正如评论中的链接(in-ulm.de/~mascheck/various/shebang/#splitting和manpages.ubuntu.com/manpages/trusty/en/man2/execve.2.html)所指出的,此行为是特定于操作系统的,其变体包括:
结论:在您的操作系统上,您无法在shebang行上为bash指定多个参数词,即使可以,也可能不应该,因为脚本在运行时会失败不支持多个shebang参数的系统。