set 命令

查看帮助

set --help

查看已设置的flag

$ echo  $-
himBHs

设置flag

set -flag

取消设置flag

set +flag

查看使用 -o 设置的 flag

$ set -o
allexport          off
braceexpand        on
emacs              on
errexit            off
errtrace           off
functrace          off
hashall            on
histexpand         on
history            on
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

使用 -o 设置 flag

# 好比命令行历史,set -o 查看状态变为on
set -o history

使用 +o 取消设置 flag

# 好比命令行历史,set -o 查看状态变为off
set +o history

set -v

显示 shell 所读取的输入值,再显示输出html

$ set -v
$ ls
ls
test1  test2
$ echo 123
echo 123
123

set -x

开启脚本调试shell

如下会直接打印中间变量扩展后的值,不须要再另外打印bash

//test6 文件
#!/usr/bin/bash
set -x
a=$1
b=$2

$ ./test6 q ewr er
+ ./test6 q ewr er
+ a=q
+ b=ewr

set --

会先将原有的位置参数 unset(至关于置空)。命令行

//文件 test5
#!/usr/bin/bash
set --
echo $0
echo $1
echo $2
echo $3

$ ./test5 zz xx cc
./test5
    //如下都是空行,说明不会读取原有位置参数

再以后若是 set -- 以后有参数,依次赋值给位置参数 ${1}${2}...调试

//文件 test5
#!/usr/bin/bash
set -- qq ww
echo $0
echo $1
echo $2
echo $3

$ ./test5
./test5
qq
ww
   //这里是一个空行,${3}为空

$ ./test5 zz xx cc
./test5
qq
ww
   //这里是一个空行,说明${3}不会读取脚本原有位置参数

set -

若是 set - 以后没有参数,原位置参数保持不变,正常读取;-x-v若是设置过的话,会被关闭。code

// 文件 test5
#!/usr/bin/bash
set -
echo $0
echo $1
echo $2
echo $3

$ ./test5 zz xx cc
./test5
zz
xx
cc

若是 set - 以后有参数,原位置参数 unset(置空)。htm

// 文件 test5
#!/usr/bin/bash
set - qq
echo $0
echo $1
echo $2
echo $3

$ ./test5 zz xx cc
./test5
qq

参考