我有一个 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 可能已经过头了,但我仍然很好奇为什么我不能指定多个长选项
最佳答案
重点是,您最多可以在 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 参数的系统。
关于具有多个长选项的 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28772745/