您的位置:首页 > 运维架构 > Shell

shell中的变量

2018-01-22 13:17 239 查看
一、了解shell中的变量

Linux shell中的变量分为“系统变量”和“用户自定义变量”,可以使用set命令查看系统变量

二、系统变量(使用set查看 如:$HOME、$PWD、$USER等等)

[root@localhost shell]# 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]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=154
DIRSTACK=()
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
IFS=$' \t\n'
LANG=zh_CN.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=39
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;
4000
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
OLDPWD=/opt
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=1229
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/opt/shell
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_CLIENT='192.168.126.1 52261 22'
SSH_CONNECTION='192.168.126.1 52261 192.168.126.100 22'
SSH_TTY=/dev/pts/0
TERM=vt100
UID=0
USER=root
_=./helloword.sh
colors=/etc/DIR_COLORS
[root@localhost shell]#
三、shell中的自定义变量

1、变量名=值(如:CSDN=abc)

2、等号两侧不能有空格

3、变量名一般习惯为大写

4、双引号和单引号有区别,双引号仅将空格脱意,单引号会将所有字符脱意

示例:

[root@localhost shell]# CSDN="hello $USER"
[root@localhost shell]# echo $CSDN
hello root
[root@localhost shell]# CSDN='hello $USER'
[root@localhost shell]# echo $CSDN
hello $USER
[root@localhost shell]#
5、撤销变量:使用unset

示例

[root@localhost shell]# CSDN1="ABC"
[root@localhost shell]# CSDN2="DEF"
[root@localhost shell]# unset CSDN2
[root@localhost shell]# echo $CSDN1
ABC
[root@localhost shell]# echo $CSDN2

[root@localhost shell]#
6、readonly声明的静态的变量,不能unset

[root@localhost shell]# readonly CSDN="ABCDEF"
[root@localhost shell]# unset CSDN
-bash: unset: CSDN: cannot unset: readonly variable
[root@localhost shell]# echo $CSDN
ABCDEF
[root@localhost shell]#
7、export 变量名 可以把变量提升为全局环境变量,可供其他shell程序使用

四、将命令返回值赋值给变量

1、反引号,运行里面的命令,并将结果返回。如:A=`ls -la`

示例:

[root@localhost shell]# A=`ls -la`
[root@localhost shell]# echo $A
总用量 56 drwxr-xr-x. 2 root root 4096 1月 22 19:31 . drwxr-xr-x. 4 root root 4096 1月 8 23:36 .. -rwxr-xr-x. 1 root root 29 1月 22 19:07 helloword.sh -rwxr-xr-x. 1 root root 162 1月 11 18:12 read.sh -rwxr-xr-x. 1 root root 213 1月 8 23:44 test1.sh -rwxr-xr-x. 1 root root 29 1月 10 21:18 test2.sh -rwxr-xr-x. 1 root root 49 1月 10 22:46 test3.sh -rwxr-xr-x. 1 root root 61 1月 10 22:59 test4.sh -rwxrwxrwx. 1 root root 37 1月 11 22:00 testfile -rw-------. 1 root root 12288 1月 12 17:47 .testPath.sh.swp -rw-r--r--. 1 root root 71 1月 22 19:31 val.sh -rwxr-xr-x. 1 root root 201 1月 11 18:05 while.sh
[root@localhost shell]#
2、$()等价于反引号。如A=$(ls -la)
示例:

[root@localhost shell]# B=$(ls -la)
[root@localhost shell]# echo $B
总用量 56 drwxr-xr-x. 2 root root 4096 1月 22 19:31 . drwxr-xr-x. 4 root root 4096 1月 8 23:36 .. -rwxr-xr-x. 1 root root 29 1月 22 19:07 helloword.sh -rwxr-xr-x. 1 root root 162 1月 11 18:12 read.sh -rwxr-xr-x. 1 root root 213 1月 8 23:44 test1.sh -rwxr-xr-x. 1 root root 29 1月 10 21:18 test2.sh -rwxr-xr-x. 1 root root 49 1月 10 22:46 test3.sh -rwxr-xr-x. 1 root root 61 1月 10 22:59 test4.sh -rwxrwxrwx. 1 root root 37 1月 11 22:00 testfile -rw-------. 1 root root 12288 1月 12 17:47 .testPath.sh.swp -rw-r--r--. 1 root root 71 1月 22 19:31 val.sh -rwxr-xr-x. 1 root root 201 1月 11 18:05 while.sh
[root@localhost shell]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息