bash有多个长选项

时间:2015-02-27 19:24:40

标签: bash

我有一个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可能过度杀戮,但我仍然很好奇为什么我不能指定多个长期选项

1 个答案:

答案 0 :(得分:2)

关键是你可以在shebang线上最多指定 一个 选项字(它与长或短选项没有任何关系)。

更确切地说,您的操作系统的execve功能会将#!/bin/bash后面的所有内容作为 单个 参数传递给bash。这导致与执行

相同
bash "--norc --noprofile"

会产生错误。

正如评论中的链接(in-ulm.de/~mascheck/various/shebang/#splittingmanpages.ubuntu.com/manpages/trusty/en/man2/execve.2.html)所指出的,此行为是特定于操作系统的,其变体包括:

  • 所有单词作为单个参数(您的情况)
  • 每个单词作为单独的参数(你期待的是什么)
  • 只有第一个单词作为单个参数
  • 根本没有参数

结论:在您的操作系统上,您无法在shebang行上为bash指定多个参数词,即使可以,也可能不应该,因为脚本在运行时会失败不支持多个shebang参数的系统。

相关问题