关于shell:set-命令

35次阅读

共计 1224 个字符,预计需要花费 4 分钟才能阅读完成。

查看帮忙

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 所读取的输出值,再显示输入

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

set -x

开启脚本调试

以下会间接打印两头变量扩大后的值,不须要再另外打印

//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 如果设置过的话,会被敞开。

// 文件 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/…

正文完
 0