next up previous contents
Next: 實機練習題 Up: Shell 變數 Previous: 環境變數使用範例   Contents

自訂變數導出 export

  1. 取得 shell 後自訂的變數,只限目前的 shell 有效,其他 shell 或其子程序 shell 都無法使用。
    [dywang@dywmac ~]$ myip=192.168.122.1
    [dywang@dywmac ~]$ echo $myip
    192.168.122.1
    [dywang@dywmac ~]$ bash
    [dywang@dywmac ~]$ echo $myip
    
    [dywang@dywmac ~]$ exit
    exit
    
  2. 若要讓目前設定的變數可以讓子程序 shell 使用,必須使用 export 導出。export 不加參數列出所有導出 (export) 的變數。
    [dywang@dywmac ~]$ export
    declare -x CVS_RSH="ssh"
    declare -x G_BROKEN_FILENAMES="1"
    declare -x HISTCONTROL="ignoredups"
    declare -x HISTSIZE="1000"
    declare -x HOME="/home/dywang"
    declare -x HOSTNAME="dywmac"
    declare -x LANG="en_US.UTF-8"
    declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
    declare -x LOGNAME="dywang"
    declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:...
    declare -x MAIL="/var/spool/mail/dywang"
    declare -x OLDPWD
    declare -x /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin"
    declare -x PWD="/home/dywang"
    declare -x SELINUX_LEVEL_REQUESTED=""
    declare -x SELINUX_ROLE_REQUESTED=""
    declare -x SELINUX_USE_CURRENT_RANGE=""
    declare -x SHELL="/bin/bash"
    declare -x SHLVL="1"
    declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"
    declare -x SSH_CLIENT="::1 50346 22"
    declare -x SSH_CONNECTION="::1 50346 ::1 22"
    declare -x SSH_TTY="/dev/pts/11"
    declare -x TERM="xterm"
    declare -x USER="dywang"
    declare -x XMODIFIERS="@im=gcin"
    
  3. 設定 myip 變數並以 export 來導出變數,進入子程式後 myip 變數仍然有效。
    [dywang@dywmac ~]$ export myip=192.168.122.1
    [dywang@dywmac ~]$ export | grep myip
    declare -x myip="192.168.122.1"
    [dywang@dywmac ~]$ bash
    [dywang@dywmac ~]$ echo $myip
    192.168.122.1
    [dywang@dywmac ~]$ exit
    exit
    



De-Yu Wang 2019-06-27