next up previous contents
Next: set 基本用法 Up: Shell Scripts Previous: shell script 練習   Contents

set 命令

  1. set 指令用來修改 shell 環境變數,是 Bash 脚本的重要工具,適當的使用可以增加脚本的安全性和可維護性。set 不加任何選項會列出所有環境變及 shell 函數。
    [dywang@dywmac zzz]$ set | grep ^H
    HISTCONTROL=ignoredups
    HISTFILE=/home/dywang/.bash_history
    HISTFILESIZE=1000
    HISTSIZE=1000
    HOME=/home/dywang
    HOSTNAME=dywmac
    HOSTTYPE=x86_64
    
  2. set 是 shell 內建的命令,要用 sh -c "help set" 查看輔助說明,選項說明如下:
    [dywang@dywmac zzz]$ sh -c "help set"
    set: set [--abefhkmnptuvxBCHP] [-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 follow symbolic links when executing commands
              such as cd which change the current directory.
          -T  If set, the DEBUG trap is inherited by shell functions.
          -   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.
    
    選項說明:
        -a  標示已修改的變數,以供輸出至環境變數。
        -b  使被中止的後台程序立刻回報執行狀態。
        -e  若指令傳回值不等於 0,則立即退出 shell。
        -H	 Shell 可利用"!"加<指令編號>的方式來執行 history 中記錄的指令。
        -k  指令所給的參數都會被視為此指令的環境變數。
        -l  記錄 for 迴圈的變數名稱。
        -m  使用監視模式。
        -n   不要執行 script,僅查詢語法的問題。
        -p  啟動優先順序模式。
        -P  啟動 -P 選項後,執行指令時,會以實際的文件或目錄來取代符號連接。
        -t  執行完隨後的指令,即退出 shell。
        -u  當執行時使用到未定義過的變數,則顯示錯誤信息。
        -v   執行 sccript 前,先將 script 的內容輸出到螢幕上;
        -x  執行指令後,會先顯示該指令及所下的選項。
    



2019-06-17