shell高级技巧:用set命令设置bash的选项

下面为set主要选项的列表及其表述:
选项名 开关缩写 描述
allexport -a 打开此开关,所有变量都自动输出给子Shell。
noclobber -C 防止重定向时文件被覆盖。
noglob -d 在路径和文件名中,关闭通配符。
    #打开该选项
    [root@xieqichao ~]# set -o allexport   #等同于set -a
    #关闭该选项
    [root@xieqichao ~]# set +o allexport  #等同于set +a
    #列出当前所有选项的当前值。
    [root@xieqichao ~]# set -o
    allexport         off
    braceexpand   on
    emacs             on
    errexit            off
    errtrace          off
    functrace        off
    hashall            on
    histexpand      on
    ... ...
    [root@xieqichao ~]# set -o noclobber     #打开noclobber选项,防止在重定向时原有文件被覆盖。
    [root@xieqichao ~]# date > outfile         #通过date命令先生成一个文件outfile。
    [root@xieqichao ~]# ls > outfile             #将ls命令的输出重定向到该文件outfile,shell将提示不能覆盖已经存在的文件。
    -bash: outfile: cannot overwrite existing file
    [root@xieqichao ~]# set +o noclobber    #关闭noclobber选项。
    [root@xieqichao ~]# ls > outfile             #重新将ls的输出重定向到outfile,成功。
发布了350 篇原创文章 · 获赞 52 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xie_qi_chao/article/details/105039108