#Bash 的 set 命令
set [OPTION]... [ARG]...
功能
设置参数列表或 shell 的特性。
类型
Bash 内置命令。
参数
OPTION选项:-a- 变量自动导出(export)-b- 后台作业完成时立即通知-e- 如果命令以非零状态退出,则立即退出 shell-f- 关闭文件名通配符功能-h- 记住命令的位置,以便查找它们;通常默认开启-k- 命令中形如NAME=VALUE的参数视作变量赋值,而非参数-m- 开启作业控制;通常默认开启-n- 读取命令并检查语法,但不执行-p- 启用特权模式(privileged mode);忽略初始化文件-t- 只执行一条命令,然后退出-u- 将未定义的变量视作错误-v- 详细模式;打印 shell 的所有输入(包括命令行)-x- 调试模式;打印执行的命令行-B- 开启大括号扩展功能;通常默认开启-C- 禁止“覆盖重定向”;文件存在时>将失败-E- 内层 shell 的ERR trap 将向外传递-H- 启用!前缀执行历史命令;通常默认开启-P- 内置命令不跟随符号链接-T- 内层 shell 的DEBUG 和 RETURN trap 将向外传递+a--a选项的逆操作+b--b选项的逆操作+e--e选项的逆操作+f--f选项的逆操作+h--h选项的逆操作+k--k选项的逆操作+m--m选项的逆操作+n--n选项的逆操作+p--p选项的逆操作+t--t选项的逆操作+u--u选项的逆操作+v--v选项的逆操作+x--x选项的逆操作+B--B选项的逆操作+C--C选项的逆操作+E--E选项的逆操作+H--H选项的逆操作+P--P选项的逆操作+T--T选项的逆操作-o OPT- 设置选项:allexport- 同-abraceexpand- 同-Bemacs- 使用 Emacs 风格的行编辑界面errexit- 同-eerrtrace- 同-Efunctrace- 同-Thashall- 同-hhistexpand- 同-Hhistory- 启用命令历史记录ignoreeof- 忽略 EOF(shell 读取到 EOF 时不退出)interactive-comments- 允许在交互式命令中显示注释keyword- 同-kmonitor- 同-mnoclobber- 同-Cnoexec- 同-nnoglob- 同-fnolog-被忽略notify- 同-bnounset- 同-uonecmd- 同-tphysical- 同-Ppipefail- 管道操作返回首个非 0 的返回值;默认情况下管道返回最后一个命令的返回值posix- 尽可能遵循 POSIX 标准privileged- 同-pverbose- 同-vvi- 使用 vi 风格的行编辑界面xtrace- 同-x
ARG- 剩余参数,会被依次赋值给$1,$2...
#示例
修改参数列表
$ set x y z
$ echo $1 $2 $3
x y z
自动导出变量
$ NAME=primers # 没有导出,新的 shell 里没有这个变量
$ bash -c 'echo $NAME'
$ set -a # 开启变量自动导出
$ NAME=primers
$ bash -c 'echo $NAME'
primers
记录命令的位置
$ type arch # 查看命令的类型,返回命令的路径
arch is /usr/bin/arch
$ arch # 执行一次该命令
x86_64
$ type arch # 再次查看命令的类型,显示命令被哈希缓存,不再从 PATH 中进行查找
arch is hashed (/usr/bin/arch)
将命令中的变量赋值视作环境变量
$ NAME=primers bash -c 'echo $NAME' # 写在命令之前的变量会成功该命令的环境变量
primers
$ bash -c 'echo $NAME' NAME=primers # 写在命令之后的不会被视作变量,而是作为命令的参数传入
$ set -k # 使命令行中的所有赋值语句都视作创建变量
$ bash -c 'echo $NAME' NAME=primers
primers
大括号扩展功能
$ echo {1..8} # 生成序列
1 2 3 4 5 6 7 8
$ echo file{1,2,3}.txt # 生成列表
file1.txt file2.txt file3.txt
$ echo {x,y,z}{1..5} # 组合生成
x1 x2 x3 x4 x5 y1 y2 y3 y4 y5 z1 z2 z3 z4 z5
! 历史命令
$ echo https://xplanc.org/primers/
https://xplanc.org/primers/
$ !e # 执行最近一条以 e 开头的命令
echo https://xplanc.org/primers/
https://xplanc.org/primers/
调试脚本
set -x # 调试模式,打印每条命令
...
set -v # 详细模式,打印命令和所有输入
...
遇到错误时立即退出脚本
set -e # 任何命令返回非 0 时,立即退出当前脚本
...
#手册
set: set [-abefhkmnptuvxBCEHPT] [-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 resolve symbolic links when executing commands
such as cd which change the current directory.
-T If set, the DEBUG and RETURN traps are 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.