本文为 Bash Reference Manual 第6章:Bash Features 第1节:Invoking Bash 的读书笔记。
完整的笔记目录参见Bash学习笔记总目录。
6.1 Invoking Bash
bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o option]
[-O shopt_option] [argument ...]
bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o option]
[-O shopt_option] -c string [argument ...]
bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o option]
[-O shopt_option] [argument ...]
所有与 set 内建命令一起使用的单字符选项(参见Set 内建命令)都可以在调用 shell 时使用。此外,还有一些可以使用的多字符选项。这些多字符选项必须出现在单字符选项之前的命令行上才能被识别。
–debugger 安排在 shell 启动前执行调试器配置文件。开启扩展调试模式(有关 extdebug 选项的描述,请参见 shopt 内建命令的说明)。
–dump-po-strings 以 GNU gettext PO(可移植对象)文件格式在标准输出上打印所有以‘$’开头的双引号字符串列表。与 -D 相同,但输出格式不同。
–dump-strings 与 -D 相同。
–help 在标准输出上显示使用信息并成功退出。
$ bash –help
GNU bash, version 5.3.0(1)-release-(x86_64-pc-linux-gnu)
Usage: bash [GNU long option] [option] ...
bash [GNU long option] [option] script-file ...
GNU long options:
–debug
–debugger
–dump-po-strings
–dump-strings
–help
–init-file
–login
–noediting
–noprofile
–norc
–posix
–pretty-print
–rcfile
–restricted
–verbose
–version
Shell options:
-ilrsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCEHPT or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
Use the `bashbug' command to report bugs.
bash home page: <http://www.gnu.org/software/bash>
General help using GNU software: <http://www.gnu.org/gethelp/>
–init-file filename –rcfile filename 在交互式 shell 中执行来自文件名的命令(而不是 ~/.bashrc)。
–login 等同于 -l。
–noediting 在 shell 为交互模式时,不使用 GNU Readline 库(参见命令行编辑)来读取命令行。
–noprofile 当 Bash 作为登录 Shell 调用时,不加载系统范围的启动文件 /etc/profile 以及任何个人初始化文件 /.bash_profile、/.bash_login 或 ~/.profile。
–norc 在交互式 Shell 中不读取 ~/.bashrc 初始化文件。如果 Shell 是以 sh 调用的,则默认启用此选项。
–posix 启用 POSIX 模式;更改 Bash 的行为,使默认操作与 POSIX 标准不同的地方与标准一致。此选项旨在使 Bash 的行为严格作为该标准的超集。有关 Bash POSIX 模式的描述,请参见 Bash 和 POSIX。
–restricted 等同于 -r。将 Shell 设置为受限 Shell(参见受限 Shell)。
–verbose 等同于 -v。读取时打印 shell 输入行。
–version 显示此 Bash 实例的版本信息到标准输出并成功退出。
在调用时可以提供几个单字符选项,而 set 内置命令无法使用这些选项。
-c 从第一个非选项参数 command_string 中读取并执行命令,然后退出。如果在 command_string 之后有参数,第一个参数会赋值给 $0,其余参数会赋值给位置参数。赋值给 $0 会设置 shell 的名称,该名称会在警告和错误信息中使用。
示例:
$ bash -c 'echo "arg0: $0"; echo "arg#: $#"; echo "args: $@"' "a b c"
arg0: a b c
arg#: 0
args:
$ bash -c 'echo "arg0: $0"; echo "arg#: $#"; echo "args: $@"' "a b c" "1 2 3"
arg0: a b c
arg#: 1
args: 1 2 3
$ bash -c 'echo "arg0: $0"; echo "arg#: $#"; echo "args: $@"' "a b c" 1 2 3
arg0: a b c
arg#: 3
args: 1 2 3
# 以下命令没有输出
$ bash -c echo a b c
# 正确的执行方式
$ bash -c "echo a b c"
a b c
💡 bash -c的正确使用方式是bash -c "command_string"或bash -c ‘command_string’。例如:
$ bash -c "echo arg0 is $0;./bashdemo.sh x y" "a b c" 1 2 3
arg0 is -bash
script:arg0 is ./bashdemo.sh
script:arg# is 2
script:arg@ is x y
$ bash -c 'echo arg0 is $0;./bashdemo.sh x y' "a b c" 1 2 3
arg0 is a b c
script:arg0 is ./bashdemo.sh
script:arg# is 2
script:arg@ is x y
以上输出不同的关键在于,双引号中的变量会展开,详见3.1.2.3 Double Quotes。
-i 强制 shell 以交互模式运行。交互式 shell 的相关内容请参见交互式 shell。
-l 使此 shell 表现得好像是由登录直接调用的。当 shell 为交互式时,这相当于使用 ‘exec -l bash’ 启动一个登录 shell。当 shell 不是交互式时,它将读取并执行登录 shell 启动文件。‘exec bash -l’ 或 ‘exec bash –login’ 将用 Bash 登录 shell 替换当前 shell。有关登录 shell 的特殊行为,请参见 Bash 启动文件。
-r 使 shell 成为受限 shell(见受限 Shell)。
-s 如果存在此选项,或者在选项处理后没有剩余参数,则 Bash 将从标准输入读取命令。此选项允许在调用交互式 shell 或通过管道读取输入时设置位置参数。
$ bash -s a b c
$ echo $@
a b c
$ echo 'echo $@' | bash -s a b c
a b c
$ echo 'echo Hello World' | bash -s
Hello World
## 通过管道读取输入
$
cat << 'EOF' | bash -s 10 20
add() {
echo "$1 + $2 = $(($1 + $2))"
}
multiply() {
echo "$1 * $2 = $(($1 * $2))"
}
echo "Use args: $1, $2"
add $1 $2
multiply $1 $2
EOF
Use args: 10, 20
10 + 20 = 30
10 * 20 = 200
-D 在标准输出上打印所有以‘$’开头的双引号字符串列表。这些字符串在当前语言环境不是 C 或 POSIX 时会进行语言翻译(参见特定语言环境翻译)。这意味着使用了 -n 选项;不会执行任何命令。
[- ]O [shopt_option] shopt_option 是 shopt 内建命令接受的一个 shell 选项(参见 The Shopt Builtin)。如果提供了 shopt_option,-O 会设置该选项的值;O 会取消设置它。如果未提供 shopt_option,Bash 会在标准输出上打印 shopt 接受的 shell 选项的名称和值。如果调用选项是 O,输出将以可以重新用作输入的格式显示。
$ echo $$ $BASHPID $BASHOPTS
8036 8036 checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
$ echo $$ $BASHPID $BASHOPTS|grep cdspell
$ bash -O
array_expand_once off
assoc_expand_once off
autocd off
bash_source_fullpath off
cdable_vars off
cdspell off
checkhash off
checkjobs off
checkwinsize on
cmdhist on
compat31 off
compat32 off
compat40 off
compat41 off
compat42 off
compat43 off
compat44 off
complete_fullquote on
direxpand off
dirspell off
dotglob off
execfail off
expand_aliases off
extdebug off
extglob off
extquote on
failglob off
force_fignore on
globasciiranges on
globskipdots on
globstar off
gnu_errfmt off
histappend off
histreedit off
histverify off
hostcomplete on
huponexit off
inherit_errexit off
interactive_comments on
lastpipe off
lithist off
localvar_inherit off
localvar_unset off
login_shell off
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
noexpand_translation off
nullglob off
patsub_replacement on
progcomp on
progcomp_alias off
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
varredir_close off
xpg_echo off
$ bash +O
shopt -u array_expand_once
shopt -u assoc_expand_once
shopt -u autocd
shopt -u bash_source_fullpath
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -u checkjobs
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u compat32
shopt -u compat40
shopt -u compat41
shopt -u compat42
shopt -u compat43
shopt -u compat44
shopt -s complete_fullquote
shopt -u direxpand
shopt -u dirspell
shopt -u dotglob
shopt -u execfail
shopt -u expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -s globasciiranges
shopt -s globskipdots
shopt -u globstar
shopt -u gnu_errfmt
shopt -u histappend
shopt -u histreedit
shopt -u histverify
shopt -s hostcomplete
shopt -u huponexit
shopt -u inherit_errexit
shopt -s interactive_comments
shopt -u lastpipe
shopt -u lithist
shopt -u localvar_inherit
shopt -u localvar_unset
shopt -u login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u noexpand_translation
shopt -u nullglob
shopt -s patsub_replacement
shopt -s progcomp
shopt -u progcomp_alias
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u varredir_close
shopt -u xpg_echo
# cdspell shopt option is disabled by default
$ bash -O cdspell
# now we can see cdspell is enabled
$ echo $BASHOPTS |grep cdspell
cdspell:checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:globskipdots:histappend:interactive_comments:patsub_replacement:progcomp:promptvars:sourcepath
# $- is the same since it is set options
$ echo $-
himBHs
💡 bash ±O不会启用一个新的shell – – 表示选项结束并禁用进一步的选项处理。– 之后的任何参数都被视为 shell 脚本文件名(参见 Shell Scripts)以及传递给该脚本的参数。
– 等同于 –。
交互式 shell 是指在没有非选项参数启动的情况下启动的 shell(除非指定了 -s),没有指定 -c 选项,并且其标准输入和标准错误都连接到终端(由 isatty(3) 确定),或者是使用 -i 选项启动的 shell。有关详细信息,请参见交互式 Shell。
如果在处理选项后仍有参数,并且既未提供 -c 选项也未提供 -s 选项,则第一个参数被视为包含 shell 命令的文件名(参见 Shell 脚本)。当 Bash 以这种方式调用时,$0 被设置为该文件的名称,位置参数被设置为剩余的参数。Bash 会读取并执行该文件中的命令,然后退出。Bash 的退出状态为脚本中最后执行命令的退出状态。如果没有执行任何命令,则退出状态为 0。Bash 首先尝试在当前目录中打开文件,如果未找到该文件,则在 PATH 目录中搜索脚本。
