一、变量命名
1、只能包含字母、数字和下划线,并且不能以数字开头,
2、不应该跟系统中已有的环境变量重名
3、命名要有含义
二、变量赋值
变量名称=值var_name=value 等号两边不能有空格
[[email protected] ~]# NAME=jerry [[email protected] ~]# echo $NAME jerry
三、bash:变量类型
1、环境变量
脚本在执行时会启动一个子shell进程。命令行中启动的脚本会继承当前shell环境变量,也就是说环境变量的作用域为当前shell进程及其子shell进程。系统自动执行的脚本(非命令行启动)则需要自我定义各环境变量。可以使用export设置环境变量。
export VARNAME=VALUE 或者 VARNAME=VALUE export VARNAME
[[email protected] ~]# NAME=jerry [[email protected] ~]# export NAME [[email protected] ~]# echo $NAME jerry [[email protected] ~]# bash [[email protected] ~]# echo $NAME jerry [[email protected] ~]# bash [[email protected] ~]# echo $NAME jerry[[email protected] ~]# pstreeinit-+-NetworkManager-+-dhclient ... |-sshd---sshd---sshd---bash---su---bash---bash---bash---pstree |-udevd---2*[udevd] `-wpa_supplican
2、本地变量(局部变量)
变量一定是进程的变量,因此在一个shell中声明的变量在其他的shell中不可用,并且在其子shell中也不可以得到。引用变量要加${varname},也可以不加{}。
[[email protected] ~]# NAME=jerry [[email protected] ~]# echo $NAME jerry [[email protected] ~]# bash [[email protected] ~]# echo $NAME [[email protected] ~]# exit exit [[email protected] ~]# echo $NAME jerry
本地变量:VARNAME=VALUE,作用域:整个bash进程。
局部变量 :local VARNAME=VALUE,作用域:当前代码段。
3、位置变量
$n
4、特殊变量(系统变量)
用来保存某些特殊数据:
$?:保存上一个命令的执行状态返回值(0-255)。0为正确,其他为错误。其中1,2,127系统预留,其他的可以自定义。
[[email protected] ~]# ls /var account crash db games lib lock mail opt run tmp yp cache cvs empty gdm local log nis preserve spool www [[email protected] ~]# echo $? 0 [[email protected] ~]# ls /varr ls: cannot access /varr: No such file or directory [[email protected] ~]# echo $? 2
如果只想看返回的状态值而不想看输出结果,则只需将输出重定向至数据黑洞/dev/null
[[email protected] ~]# ls /varr &> /dev/null [[email protected] ~]# echo $? 2
四、撤销变量
[[email protected] ~]# unset NAME [[email protected] ~]# echo $NAME
五、查看当前shell变量 查看当前shell所有变量,包含环境变量和本地变量
[[email protected] ~]# set BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() ...
查看当前shell环境变量 printenv env 或者export
[[email protected] ~]# printenv HOSTNAME=hao SHELL=/bin/bash TERM=xterm ...
六、追加shell变量
[[email protected] ~]# ANIMAL=pig [[email protected] ~]# ANIMAL=$ANIMAL:goat [[email protected] ~]# echo $ANIMAL pig:goat [[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] ~]# export PATH=$PATH:/usr/local/apache/bin [[email protected] ~]# echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin [[email protected] ~]# export PATH=/usr/local/mysql/bin:$PATH [[email protected] ~]# echo $PATH /usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin
七、第一个脚本程序
首先创建脚本程序first.sh,脚本程序的第一行要指定程序的魔数,#!加上解释器的路径。然后赋予其执行权限,之后执行,执行时候要加上路径,因为在没有给路径时,默认去path中去找该命令是否存在。
[[email protected] ~]# cat first.sh #!/bin/bash ls /var cat /etc/inittab [[email protected] ~]# chmod +x first.sh [[email protected] ~]# ./first.sh account crash db games lib lock mail opt run tmp yp cache cvs empty gdm local log nis preserve spool www # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:5:initdefault: