set并且shopt都是控制各种选项的shell内置程序。我经常忘记由哪个命令设置哪些选项,以及设置/取消设置哪些选项(set -o/+o,shopt -s/-u)。为什么会有两个不同的命令看似执行相同的操作(并且具有不同的参数来执行此操作)?有什么简单的方法/助记符来记住哪个选项与哪个命令一起使用?
set并且shopt都是控制各种选项的shell内置程序。我经常忘记由哪个命令设置哪些选项,以及设置/取消设置哪些选项(set -o/+o,shopt -s/-u)。为什么会有两个不同的命令看似执行相同的操作(并且具有不同的参数来执行此操作)?有什么简单的方法/助记符来记住哪个选项与哪个命令一起使用?
Answers:
据我所知,set -o选项是从其他Bourne样式的shell(主要是ksh)继承的shopt选项,而这些选项是bash特有的选项。我没有逻辑可言。
shopt都是继承的吗?
                    set -o像posix/ physical/的选项interactive-comments不在in中ksh,而有些shopt则在其他shell中,包括ksh像login_shell/这样的nullglob。就像你说的那样,没有逻辑。最初可能是想法(SHELLOPTS是标准的,BASHOPTS是bash的特定想法),但是一路迷失了,现在最终变得烦人,UI设计惨败。
                    区别在于bash使用的已更改环境变量。使用set命令进行设置会导致$SHELLOPTS。使用shopt命令进行设置会导致$BASHOPTS。
shopt与$ SH ELL OPT做得相当超过$ BA 禁用了javascript S.
                    set是POSIX 7:设置-设置或未设置的选项和位置参数| pubs.opengroup.org
可能与@Gilles提到的历史有关。
容易,但历史悠久。该set命令最初用于修改原始unix shell的命令行环境/bin/sh。然后,随着各种Unix版本的发展以及增加了新的shell风格,人们意识到他们需要能够更改更多(环境)内容,以保持shell脚本兼容。当时猛砸变得非常流行和附加SH ELL 选择需要离子导入shopt。
您实际上可以在命令中看到这些兼容性尝试shopt。
$ shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    off
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
complete_fullquote      on
direxpand       off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globstar        off
globasciiranges off
gnu_errfmt      off
histappend      on
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lastpipe        off
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      on
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
但是set命令中没有。
$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
igncr           off
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off
              set作为设置选项的方法不是最初的Unix shell所采用,它是Bourne shell在70年代后期引入的。set -o name本身是后来由Korn shell添加的,在POSIX中已指定但是可选的,但仍不受Bourne shell 的“现代”版本(如/bin/shSolaris 10)的支持。
                    看起来“设置”选项是由子Shell继承的,而Shopt则不是。
set不被subshells.Both继承set和shopt选项不是由子shell继承。
                    set和shopt?
                    set -o和shopt选项由子shell(继承(...),$(...),管道元件)。无论他们是通过其他遗传bash调用取决于是否SHELLOPTS或者BASHOPTS在环境中或不是。
                    set源于bourne shell(sh),是POSIX标准的一部分,shopt但是不是,并且是bourne-again shell(bash)特定的:
0 sjas@ssg 14:31:45 ~  
set | grep -e SHELLOPTS -e BASHOPTS
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:dotglob:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
0 sjas@ssg 14:31:51 ~  
shopt | column -t | grep -v off
checkwinsize             on
cmdhist                  on
complete_fullquote       on
dotglob                  on
expand_aliases           on
extglob                  on
extquote                 on
force_fignore            on
histappend               on
interactive_comments     on
progcomp                 on
promptvars               on
sourcepath               on
0 sjas@ssg 14:31:57 ~  
set -o | column -t | grep -v off
braceexpand           on
emacs                 on
hashall               on
histexpand            on
history               on
interactive-comments  on
monitor               on
0 sjas@ssg 14:37:41 ~ 
sh 
$ set -o
Current option settings
errexit         off
noglob          off
ignoreeof       off
interactive     on
monitor         on
noexec          off
stdin           on
xtrace          off
verbose         off
vi              off
emacs           off
noclobber       off
allexport       off
notify          off
nounset         off
priv            off
nolog           off
debug           off
$ shopt
sh: 3: shopt: not found
$ 
              
help set并help shopt验证甚至他们的作者也认为他们做同样的事情。