首页 » 信息技术 »

echo $-

2019年2月16日 / 32次阅读
Bash

  • 打开支付宝,搜索“ 529018372 ”,领取专属红包!每日支付每日领。

正常情况,我们在Bash命令行中输入echo $-,看到的是如下奇怪的输出:

[xinlin@localhost ~]$ echo $-
himBH

$-是Bash内置的一个特殊变量,就是$$(当前Bash的pid)一样,$-代表的是当前Bash的运行选项(options),这些Bash选项控制着Bash运行时的行为。

himBH的解释如下:

h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.
i: The current shell is interactive
m: Job control is enabled
B: Brace expansion is enabled
H: History substitution like !-1

完整的Bash options,可以通过bash -c "help set"命令获取(set是Bash的一个builtins):

[xinlin@localhost ~]$ bash -c "help set"
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
Set or unset values of shell options and positional parameters.

Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.

Options:
-a Mark variables which are modified or created for export.
-b Notify of job termination immediately.
-e Exit immediately if a command exits with a non-zero status.
-f Disable file name generation (globbing).
-h Remember the location of commands as they are looked up.
-k All assignment arguments are placed in the environment for a
command, not just those that precede the command name.
-m Job control is enabled.
-n Read commands but do not execute them.
-o option-name
Set the variable corresponding to option-name:
allexport same as -a
braceexpand same as -B
emacs use an emacs-style line editing interface
errexit same as -e
errtrace same as -E
functrace same as -T
hashall same as -h
histexpand same as -H
history enable command history
ignoreeof the shell will not exit upon reading EOF
interactive-comments
allow comments to appear in interactive commands
keyword same as -k
monitor same as -m
noclobber same as -C
noexec same as -n
noglob same as -f
nolog currently accepted but ignored
notify same as -b
nounset same as -u
onecmd same as -t
physical same as -P
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
posix change the behavior of bash where the default
operation differs from the Posix standard to
match the standard
privileged same as -p
verbose same as -v
vi use a vi-style line editing interface
xtrace same as -x
-p Turned on whenever the real and effective user ids do not match.
Disables processing of the $ENV file and importing of shell
functions. Turning this option off causes the effective uid and
gid to be set to the real uid and gid.
-t Exit after reading and executing one command.
-u Treat unset variables as an error when substituting.
-v Print shell input lines as they are read.
-x Print commands and their arguments as they are executed.
-B the shell will perform brace expansion
-C If set, disallow existing regular files to be overwritten
by redirection of output.
-E If set, the ERR trap is inherited by shell functions.
-H Enable ! style history substitution. This flag is on
by default when the shell is interactive.
-P If set, do not follow symbolic links when executing commands
such as cd which change the current directory.
-T If set, the DEBUG trap is inherited by shell functions.
-- Assign any remaining arguments to the positional parameters.
If there are no remaining arguments, the positional parameters
are unset.
- Assign any remaining arguments to the positional parameters.
The -x and -v options are turned off.

Using + rather than - causes these flags to be turned off. The
flags can also be used upon invocation of the shell. The current
set of flags may be found in $-. The remaining n ARGs are positional
parameters and are assigned, in order, to $1, $2, .. $n. If no
ARGs are given, all shell variables are printed.

Exit Status:
Returns success unless an invalid option is given.

注意:上面的显示没有-i选项(Bash interactive mode)。

做一个non-interactive mode的测试:

[xinlin@localhost ~]$ echo 'echo $-' > test.sh
[xinlin@localhost ~]$ bash test.sh
hB

在non-interactive mode下,bash的options默认就只剩下hB。

以上就是对echo $-的总结。

本文链接:https://www.maixj.net/ict/bash-options-20169

相关文章

留言区

《echo $-》有1条留言

  • 麦新杰

    通过builtin的set命令,可以设置或取消某些bash的选项,比如设置set -h,取消set +h。注意,取消时,反而要用+号,这是因为默认设置是-。 []


前一篇:
后一篇:
推一篇:可靠正规,长期稳定,网络兼职项目!!

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目


©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top