1. 程式人生 > >Linux學習之Shell程式設計——Bash變數——環境變數

Linux學習之Shell程式設計——Bash變數——環境變數

1、環境變數,一部分是系統自定義的,還有一部分是使用者新增的,使用者可以改變其值

》環境變數是什麼?

使用者自定義變數只在當前的shell中生效,而環境變數會在當前Shell和這個Shell的所有子Shell當中生效。如果把環境變數寫入相應的配置檔案,那麼這個環境變數就會在所有的Shell中生效。。

2、設定環境變數

》export 變數名=變數值     (注意等號兩邊不能加空格)

#申明變數

export  表示申明

1)如何定義子shell?

在當前shell中 直接通過另一個shell名稱,即可新啟用一個shell,這就是子shell,

在該子shell中,通過輸入exit 即可退出當前子shell

2)如何查詢當前shell?

     └─wpa_supplicant
[[email protected] ~]# 
[[email protected] ~]# csh
[[email protected] ~]# bash
[[email protected] ~]# csh
[[email protected] ~]# bash
[[email protected] ~]# pstree
init─┬─NetworkManager
     ├─abrtd
     ├─acpid
     ├─atd
     ├─auditd───{auditd}
     ├─automount───4*[{automount}]
     ├─bluetoothd
     ├─bonobo-activati───{bonobo-activat}
     ├─certmonger
     ├─clock-applet
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─cupsd
     ├─2*[dbus-daemon───{dbus-daemon}]
     ├─2*[dbus-launch]
     ├─devkit-power-da
     ├─gconf-im-settin
     ├─gconfd-2
     ├─gdm-binary─┬─gdm-simple-slav─┬─Xorg
     │            │                 ├─gdm-session-wor─┬─gnome-session─┬─abrt-applet
     │            │                 │                 │               ├─bluetooth-apple
     │            │                 │                 │               ├─evolution-alarm
     │            │                 │                 │               ├─gdu-notificatio
     │            │                 │                 │               ├─gnome-panel
     │            │                 │                 │               ├─gnome-power-man
     │            │                 │                 │               ├─gnome-volume-co
     │            │                 │                 │               ├─gpk-update-icon
     │            │                 │                 │               ├─metacity
     │            │                 │                 │               ├─nautilus
     │            │                 │                 │               ├─nm-applet
     │            │                 │                 │               ├─polkit-gnome-au
     │            │                 │                 │               ├─python
     │            │                 │                 │               ├─restorecond
     │            │                 │                 │               └─{gnome-session}
     │            │                 │                 └─{gdm-session-wo}
     │            │                 └─{gdm-simple-sla}
     │            └─{gdm-binary}
     ├─gdm-user-switch
     ├─gnome-keyring-d───2*[{gnome-keyring-}]
     ├─gnome-screensav
     ├─gnome-settings-───{gnome-settings}
     ├─gnote
     ├─gvfs-afc-volume───{gvfs-afc-volum}
     ├─gvfs-fuse-daemo───3*[{gvfs-fuse-daem}]
     ├─gvfs-gdu-volume
     ├─gvfs-gphoto2-vo
     ├─gvfsd
     ├─gvfsd-burn
     ├─gvfsd-trash
     ├─hald─┬─hald-runner─┬─hald-addon-acpi
     │      │             ├─hald-addon-inpu
     │      │             └─hald-addon-rfki
     │      └─{hald}
     ├─ibus-x11
     ├─im-settings-dae─┬─ibus-daemon─┬─ibus-engine-pin
     │                 │             ├─ibus-gconf
     │                 │             ├─python
     │                 │             └─{ibus-daemon}
     │                 └─{im-settings-da}
     ├─master─┬─pickup
     │        └─qmgr
     ├─5*[mingetty]
     ├─modem-manager
     ├─notification-ar
     ├─polkitd
     ├─pulseaudio─┬─gconf-helper
     │            └─2*[{pulseaudio}]
     ├─rpc.statd
     ├─rpcbind
     ├─rsyslogd───3*[{rsyslogd}]
     ├─rtkit-daemon───2*[{rtkit-daemon}]
     ├─seahorse-daemon
     ├─sshd─┬─2*[sshd───bash]
     │      └─sshd───bash───csh───bash───csh───bash───pstree
     ├─trashapplet
     ├─udevd───2*[udevd]
     ├─udisks-daemon─┬─udisks-daemon
     │               └─{udisks-daemon}
     ├─wnck-applet
     └─wpa_supplicant
[
[email protected]
~]#

如上,檢視查詢結果中pstree,該節點前面有bash——csh——bash——csh——bash

那我們退出

注意,通過以下全部示例,注意set命令查詢結果中sex name 等的區別,注意pstree 中pstree的位置變化,理解其中微妙變化

[[email protected] ~]# 
[[email protected] ~]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(2)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=88
CVS_RSH=ssh
DIRSTACK=()
DISPLAY=localhost:12.0
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=localhost.localdomain
HOSTTYPE=x86_64
ID=0
IFS=$' \t\n'
LANG=zh_CN.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LINES=34
LOGNAME=root
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:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:'
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=7517
PROMPT_COMMAND='printf "\033]0;%
[email protected]
%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' PS1='[\[email protected]\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/root QTDIR=/usr/lib64/qt-3.3 QTINC=/usr/lib64/qt-3.3/include QTLIB=/usr/lib64/qt-3.3/lib SELINUX_LEVEL_REQUESTED= SELINUX_ROLE_REQUESTED= SELINUX_USE_CURRENT_RANGE= SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL=1 SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass SSH_CLIENT='192.168.1.137 51668 22' SSH_CONNECTION='192.168.1.137 51668 192.168.1.132 22' SSH_TTY=/dev/pts/2 TERM=xterm UID=0 USER=root _=123456789 aa=123456789 abc=$'2018\345\271\264 12\346\234\210 16\346\227\245 \346\230\237\346\234\237\346\227\245 15:17:11 CST' colors=/etc/DIR_COLORS name=sc __udisks () { local IFS=' '; local cur="${COMP_WORDS[COMP_CWORD]}"; if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then _filedir || return 0; else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur)); fi; fi; fi; fi; fi; fi; fi; fi; fi } [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# csh [[email protected] ~]# [[email protected] ~]# exit exit [[email protected] ~]# bash [[email protected] ~]# exit exit [[email protected] ~]# pstree init─┬─NetworkManager ├─abrtd ├─acpid ├─atd ├─auditd───{auditd} ├─automount───4*[{automount}] ├─bluetoothd ├─bonobo-activati───{bonobo-activat} ├─certmonger ├─clock-applet ├─console-kit-dae───63*[{console-kit-da}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─2*[dbus-launch] ├─devkit-power-da ├─gconf-im-settin ├─gconfd-2 ├─gdm-binary─┬─gdm-simple-slav─┬─Xorg │ │ ├─gdm-session-wor─┬─gnome-session─┬─abrt-applet │ │ │ │ ├─bluetooth-apple │ │ │ │ ├─evolution-alarm │ │ │ │ ├─gdu-notificatio │ │ │ │ ├─gnome-panel │ │ │ │ ├─gnome-power-man │ │ │ │ ├─gnome-volume-co │ │ │ │ ├─gpk-update-icon │ │ │ │ ├─metacity │ │ │ │ ├─nautilus │ │ │ │ ├─nm-applet │ │ │ │ ├─polkit-gnome-au │ │ │ │ ├─python │ │ │ │ ├─restorecond │ │ │ │ └─{gnome-session} │ │ │ └─{gdm-session-wo} │ │ └─{gdm-simple-sla} │ └─{gdm-binary} ├─gdm-user-switch ├─gnome-keyring-d───2*[{gnome-keyring-}] ├─gnome-screensav ├─gnome-settings-───{gnome-settings} ├─gnote ├─gvfs-afc-volume───{gvfs-afc-volum} ├─gvfs-fuse-daemo───3*[{gvfs-fuse-daem}] ├─gvfs-gdu-volume ├─gvfs-gphoto2-vo ├─gvfsd ├─gvfsd-burn ├─gvfsd-trash ├─hald─┬─hald-runner─┬─hald-addon-acpi │ │ ├─hald-addon-inpu │ │ └─hald-addon-rfki │ └─{hald} ├─ibus-x11 ├─im-settings-dae─┬─ibus-daemon─┬─ibus-engine-pin │ │ ├─ibus-gconf │ │ ├─python │ │ └─{ibus-daemon} │ └─{im-settings-da} ├─master─┬─pickup │ └─qmgr ├─5*[mingetty] ├─modem-manager ├─notification-ar ├─polkitd ├─pulseaudio─┬─gconf-helper │ └─2*[{pulseaudio}] ├─rpc.statd ├─rpcbind ├─rsyslogd───3*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─seahorse-daemon ├─sshd─┬─2*[sshd───bash] │ └─sshd───bash───pstree ├─trashapplet ├─udevd───2*[udevd] ├─udisks-daemon─┬─udisks-daemon │ └─{udisks-daemon} ├─wnck-applet └─wpa_supplicant [[email protected] ~]# [[email protected] ~]# csh [[email protected] ~]# bash [[email protected] ~]# csh [[email protected] ~]# bash [[email protected] ~]# pstree init─┬─NetworkManager ├─abrtd ├─acpid ├─atd ├─auditd───{auditd} ├─automount───4*[{automount}] ├─bluetoothd ├─bonobo-activati───{bonobo-activat} ├─certmonger ├─clock-applet ├─console-kit-dae───63*[{console-kit-da}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─2*[dbus-launch] ├─devkit-power-da ├─gconf-im-settin ├─gconfd-2 ├─gdm-binary─┬─gdm-simple-slav─┬─Xorg │ │ ├─gdm-session-wor─┬─gnome-session─┬─abrt-applet │ │ │ │ ├─bluetooth-apple │ │ │ │ ├─evolution-alarm │ │ │ │ ├─gdu-notificatio │ │ │ │ ├─gnome-panel │ │ │ │ ├─gnome-power-man │ │ │ │ ├─gnome-volume-co │ │ │ │ ├─gpk-update-icon │ │ │ │ ├─metacity │ │ │ │ ├─nautilus │ │ │ │ ├─nm-applet │ │ │ │ ├─polkit-gnome-au │ │ │ │ ├─python │ │ │ │ ├─restorecond │ │ │ │ └─{gnome-session} │ │ │ └─{gdm-session-wo} │ │ └─{gdm-simple-sla} │ └─{gdm-binary} ├─gdm-user-switch ├─gnome-keyring-d───2*[{gnome-keyring-}] ├─gnome-screensav ├─gnome-settings-───{gnome-settings} ├─gnote ├─gvfs-afc-volume───{gvfs-afc-volum} ├─gvfs-fuse-daemo───3*[{gvfs-fuse-daem}] ├─gvfs-gdu-volume ├─gvfs-gphoto2-vo ├─gvfsd ├─gvfsd-burn ├─gvfsd-trash ├─hald─┬─hald-runner─┬─hald-addon-acpi │ │ ├─hald-addon-inpu │ │ └─hald-addon-rfki │ └─{hald} ├─ibus-x11 ├─im-settings-dae─┬─ibus-daemon─┬─ibus-engine-pin │ │ ├─ibus-gconf │ │ ├─python │ │ └─{ibus-daemon} │ └─{im-settings-da} ├─master─┬─pickup │ └─qmgr ├─5*[mingetty] ├─modem-manager ├─notification-ar ├─polkitd ├─pulseaudio─┬─gconf-helper │ └─2*[{pulseaudio}] ├─rpc.statd ├─rpcbind ├─rsyslogd───3*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─seahorse-daemon ├─sshd─┬─2*[sshd───bash] │ └─sshd───bash───csh───bash───csh───bash───pstree ├─trashapplet ├─udevd───2*[udevd] ├─udisks-daemon─┬─udisks-daemon │ └─{udisks-daemon} ├─wnck-applet └─wpa_supplicant [[email protected] ~]# [[email protected] ~]# name=sc [[email protected] ~]# sex=man [[email protected] ~]# export set [[email protected] ~]# export sex [[email protected] ~]# set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu") BASH_VERSION='4.1.2(2)-release' COLORS=/etc/DIR_COLORS COLUMNS=88 CVS_RSH=ssh DIRSTACK=() DISPLAY=localhost:12.0 EUID=0 GROUP=root GROUPS=() G_BROKEN_FILENAMES=1 HISTCONTROL=ignoredups HISTFILE=/root/.bash_history HISTFILESIZE=1000 HISTSIZE=1000 HOME=/root HOST=localhost.localdomain HOSTNAME=localhost.localdomain HOSTTYPE=x86_64-linux ID=0 IFS=$' \t\n' LANG=zh_CN.UTF-8 LESSOPEN='||/usr/bin/lesspipe.sh %s' LINES=34 LOGNAME=root 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:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:' MACHTYPE=x86_64 MAIL=/var/spool/mail/root MAILCHECK=60 OPTERR=1 OPTIND=1 OSTYPE=linux PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin PIPESTATUS=([0]="0") PPID=7730 PROMPT_COMMAND='printf "\033]0;%[email protected]%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' PS1='[\[email protected]\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/root QTDIR=/usr/lib64/qt-3.3 QTINC=/usr/lib64/qt-3.3/include QTLIB=/usr/lib64/qt-3.3/lib REMOTEHOST=192.168.1.137 SELINUX_LEVEL_REQUESTED= SELINUX_ROLE_REQUESTED= SELINUX_USE_CURRENT_RANGE= SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL=5 SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass SSH_CLIENT='192.168.1.137 51668 22' SSH_CONNECTION='192.168.1.137 51668 192.168.1.132 22' SSH_TTY=/dev/pts/2 TERM=xterm UID=0 USER=root VENDOR=unknown _=sex colors=/etc/DIR_COLORS name=sc sex=man __udisks () { local IFS=' '; local cur="${COMP_WORDS[COMP_CWORD]}"; if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then _filedir || return 0; else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur)); fi; fi; fi; fi; fi; fi; fi; fi; fi } [[email protected] ~]# bash [[email protected] ~]# set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu") BASH_VERSION='4.1.2(2)-release' COLORS=/etc/DIR_COLORS COLUMNS=88 CVS_RSH=ssh DIRSTACK=() DISPLAY=localhost:12.0 EUID=0 GROUP=root GROUPS=() G_BROKEN_FILENAMES=1 HISTCONTROL=ignoredups HISTFILE=/root/.bash_history HISTFILESIZE=1000 HISTSIZE=1000 HOME=/root HOST=localhost.localdomain HOSTNAME=localhost.localdomain HOSTTYPE=x86_64-linux ID=0 IFS=$' \t\n' LANG=zh_CN.UTF-8 LESSOPEN='||/usr/bin/lesspipe.sh %s' LINES=34 LOGNAME=root 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:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:' MACHTYPE=x86_64 MAIL=/var/spool/mail/root MAILCHECK=60 OPTERR=1 OPTIND=1 OSTYPE=linux PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin PIPESTATUS=([0]="0") PPID=7738 PROMPT_COMMAND='printf "\033]0;%[email protected]%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' PS1='[\[email protected]\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/root QTDIR=/usr/lib64/qt-3.3 QTINC=/usr/lib64/qt-3.3/include QTLIB=/usr/lib64/qt-3.3/lib REMOTEHOST=192.168.1.137 SELINUX_LEVEL_REQUESTED= SELINUX_ROLE_REQUESTED= SELINUX_USE_CURRENT_RANGE= SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor SHLVL=6 SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass SSH_CLIENT='192.168.1.137 51668 22' SSH_CONNECTION='192.168.1.137 51668 192.168.1.132 22' SSH_TTY=/dev/pts/2 TERM=xterm UID=0 USER=root VENDOR=unknown _=/etc/bashrc colors=/etc/DIR_COLORS sex=man __udisks () { local IFS=' '; local cur="${COMP_WORDS[COMP_CWORD]}"; if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then _filedir || return 0; else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur)); else COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur)); fi; fi; fi; fi; fi; fi; fi; fi; fi } [ro[email protected] ~]#

#申明變數

》env

#查詢變數,這個更常用

[[email protected] ~]# env
REMOTEHOST=192.168.1.137
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
HOST=localhost.localdomain
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.1.137 51668 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/2
GROUP=root
USER=root
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:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
HOSTTYPE=x86_64-linux
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
MAIL=/var/spool/mail/root
PWD=/root
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=6
OSTYPE=linux
VENDOR=unknown
MACHTYPE=x86_64
LOGNAME=root
CVS_RSH=ssh
QTLIB=/usr/lib64/qt-3.3/lib
SSH_CONNECTION=192.168.1.137 51668 192.168.1.132 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
DISPLAY=localhost:12.0
sex=man
G_BROKEN_FILENAMES=1
_=/bin/env
[[email protected] ~]# 

PS1 變數只能通過set命令檢視,不是自定義的環境變數,而是系統預設的,目前還未用的

》unset  變數名

#刪除變數

3、系統常見環境變數

》PATH:系統查詢命令的路徑

[[email protected] ~]# echo $PATH

/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr:/bin:root/bin

》PATH="$PATH":/root/sh

#PATH變數疊加

不管是系統命令還是自定義指令碼執行,歸根結底都需要絕對路徑呼叫

[[email protected] ~]# vim hello.sh

#!/bin/bash

echo "hello world"
~                                                                                       
~                                                                                       
~      
[[email protected] ~]# 
[[email protected] ~]# ./hello.sh 
hello world
[[email protected] ~]# /root/hello.sh 
hello world
[[email protected] ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[[email protected] ~]# cp hello.sh /bin/
[[email protected] ~]# hello.sh 
hello world
[[email protected] ~]# rm -rf /root/hello.sh 
[[email protected] ~]# PATH="$PATH":/root
[[email protected] ~]# hello.sh 
hello world
[[email protected] ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root
[[email protected] ~]# 
             

以上示例表示自己建立了一個指令碼檔案hello.sh,並通過基本的賦許可權,通過絕對路徑和相對路徑的方式呼叫執行

然後通過檢視當前系統環境變數的路徑,將該指令碼放入其中某個路徑,便可直接執行這個指令碼,但不符合一般的

原則,所以我們選擇將某個路徑作為環境變數配置到PATH變數中,此時,我們便可在任意位置呼叫執行該指令碼

同時通過查詢PATH環境變數的值,發現自己新增的/root目錄在其中

4、PSI:定義系統提示符的變數  (也就是預設命令輸入前的[[email protected] ~] 的值)

\d

顯示日期,格式為“星期 月 日”

\h

顯示簡寫主機名。如預設主機名“localhost”
\t 顯示24小時制時間,格式為“HH:MM:SS”
\T 顯示12小時制時間,格式 為“HH:MM:SS”
\A 顯示24小時制時間,格式 為“HH:MM”
\u 顯示當前使用者名稱
\w 顯示當前所在目錄的完整名稱
\W 顯示當前所在目錄的最後一個目錄
\# 執行的第幾個命令
\$ 提示符。如果是root使用者會顯示提示符為“#”,如果是普通使用者會顯示提示符為“$”
[[email protected] ~]# 
[[email protected] ~]# echo $PS1
[\[email protected]\h \W]\$
[[email protected] ~]# PS1='[\[email protected]\t \w]\$ '
[[email protected]:57:10 ~]# cd /usr/local/src/
[[email protected]:57:23 /usr/local/src]# PS1='[\[email protected]\t \# \w]\$ '
[[email protected]:58:31 24 /usr/local/src]# 
[[email protected]:58:36 24 /usr/local/src]# 
[[email protected]:58:36 24 /usr/local/src]# PS1='[\[email protected]\h \W]\$'
[[email protected] src]#
[[email protected] src]#cd ~
[[email protected] ~]#