bash: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 historyset -v
显示 shell 所读取的输入值,再显示输出
$ set -v
$ ls
ls
test1 test2
$ echo 123
echo 123
123set -x
开启脚本调试
以下会直接打印中间变量扩展后的值,不需要再另外打印
//test6 文件
#!/usr/bin/bash
set -x
a=$1
b=$2
$ ./test6 q ewr er
+ ./test6 q ewr er
+ a=q
+ b=ewrset --
会先将原有的位置参数 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如果设置过的话,会被关闭。
// 文件 test5
#!/usr/bin/bash
set -
echo $0
echo $1
echo $2
echo $3
$ ./test5 zz xx cc
./test5
zz
xx
cc如果 set - 之后有参数,原位置参数 unset(置空)。
// 文件 test5
#!/usr/bin/bash
set - qq
echo $0
echo $1
echo $2
echo $3
$ ./test5 zz xx cc
./test5
qq
参考
- Bash Reference Manual:https://www.gnu.org/software/...

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论