命名要求:
命名习惯:
name='value'
value可以是以下多种形式
直接字串:name='root' 变量引用:name="$USER" 命令引用:name=`COMMAND` 或 name=$(COMMAND)
注意:变量赋值是临时生效,退出终端后,变量会自动删除,无法持久保存,脚本中的变量会随着脚本结束,也会自动删除
$name ${name}
弱引用和强引用
例:变量的各种赋值方式和引用
[root@CentOS7 ~]# TITLE='cto' [root@CentOS7 ~]# echo $TITLE cto [root@CentOS7 ~]# echo I am $TITLE I am cto [root@CentOS7 ~]# echo "I am $TITLE" I am cto [root@CentOS7 ~]# echo 'I am $TITLE' I am $TITLE [root@CentOS7 ~]# NAME=$USER [root@CentOS7 ~]# echo $NAME root [root@CentOS7 ~]# USER=`whoami` [root@CentOS7 ~]# echo $USER root [root@CentOS7 ~]# FILE=`ls /etc` [root@CentOS7 ~]# echo $FILE adjtime aliases aliases.db alternatives anacrontab asound.conf at.deny audisp audit bash_completion.d bashrc binfmt.d centos-release centos-release-upstream chkconfig.d cron.d cron.daily cron.deny cron.hourly cron.monthly crontab cron.weekly crypttab csh.cshrc csh.login dbus-1 default depmod.d dhcp DIR_COLORS DIR_COLORS.256color DIR_COLORS.lightbgcolor dracut.conf dracut.conf.d e2fsck.conf environment ethertypes exports exports.d favicon.png filesystems firewalld fstab fuse.conf gcrypt gnupg GREP_COLORS groff group group- grub2.cfg grub.d gshadow gshadow- gss gssproxy host.conf hostname hosts hosts.allow hosts.deny idmapd.conf init.d inittab inputrc iproute2 issue issue.net kdump.conf kernel krb5.conf krb5.conf.d ld.so.cache ld.so.conf ld.so.conf.d libaudit.conf libnl libuser.conf locale.conf localtime login.defs logrotate.conf logrotate.d lsb-release.d lvm machine-id magic mail.rc makedumpfile.conf.sample man_db.conf mke2fs.conf modprobe.d modules-load.d motd mtab my.cnf my.cnf.d netconfig NetworkManager networks nfs.conf nfsmount.conf nsswitch.conf nsswitch.conf.bak openldap opt os-release pam.d passwd passwd- pkcs11 pki plymouth pm polkit-1 popt.d postfix ppp prelink.conf.d printcap profile profile.d protocols python rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.d rc.local redhat-lsb redhat-release request-key.conf request-key.d resolv.conf rpc rpm rsyslog.conf rsyslog.d rwtab rwtab.d sasl2 securetty security selinux services sestatus.conf shadow shadow- shells skel ssh ssl statetab statetab.d subgid subuid sudo.conf sudoers sudoers.d sudo-ldap.conf sysconfig sysctl.conf sysctl.d systemd system-release system-release-cpe terminfo tmpfiles.d tuned udev vconsole.conf vimrc virc vmware-tools wgetrc wpa_supplicant X11 xdg xinetd.d yum yum.conf yum.repos.d [root@CentOS7 ~]# FILE=/etc/* [root@CentOS7 ~]# echo $FILE /etc/adjtime /etc/aliases /etc/aliases.db /etc/alternatives /etc/anacrontab /etc/asound.conf /etc/at.deny /etc/audisp /etc/audit /etc/bash_completion.d /etc/bashrc /etc/binfmt.d /etc/centos-release /etc/centos-release-upstream /etc/chkconfig.d /etc/cron.d /etc/cron.daily /etc/cron.deny /etc/cron.hourly /etc/cron.monthly /etc/crontab /etc/cron.weekly /etc/crypttab /etc/csh.cshrc /etc/csh.login /etc/dbus-1 /etc/default /etc/depmod.d /etc/dhcp /etc/DIR_COLORS /etc/DIR_COLORS.256color /etc/DIR_COLORS.lightbgcolor /etc/dracut.conf /etc/dracut.conf.d /etc/e2fsck.conf /etc/environment /etc/ethertypes /etc/exports /etc/exports.d /etc/favicon.png /etc/filesystems /etc/firewalld /etc/fstab /etc/fuse.conf /etc/gcrypt /etc/gnupg /etc/GREP_COLORS /etc/groff /etc/group /etc/group- /etc/grub2.cfg /etc/grub.d /etc/gshadow /etc/gshadow- /etc/gss /etc/gssproxy /etc/host.conf /etc/hostname /etc/hosts /etc/hosts.allow /etc/hosts.deny /etc/idmapd.conf /etc/init.d /etc/inittab /etc/inputrc /etc/iproute2 /etc/issue /etc/issue.net /etc/kdump.conf /etc/kernel /etc/krb5.conf /etc/krb5.conf.d /etc/ld.so.cache /etc/ld.so.conf /etc/ld.so.conf.d /etc/libaudit.conf /etc/libnl /etc/libuser.conf /etc/locale.conf /etc/localtime /etc/login.defs /etc/logrotate.conf /etc/logrotate.d /etc/lsb-release.d /etc/lvm /etc/machine-id /etc/magic /etc/mail.rc /etc/makedumpfile.conf.sample /etc/man_db.conf /etc/mke2fs.conf /etc/modprobe.d /etc/modules-load.d /etc/motd /etc/mtab /etc/my.cnf /etc/my.cnf.d /etc/netconfig /etc/NetworkManager /etc/networks /etc/nfs.conf /etc/nfsmount.conf /etc/nsswitch.conf /etc/nsswitch.conf.bak /etc/openldap /etc/opt /etc/os-release /etc/pam.d /etc/passwd /etc/passwd- /etc/pkcs11 /etc/pki /etc/plymouth /etc/pm /etc/polkit-1 /etc/popt.d /etc/postfix /etc/ppp /etc/prelink.conf.d /etc/printcap /etc/profile /etc/profile.d /etc/protocols /etc/python /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rc.d /etc/rc.local /etc/redhat-lsb /etc/redhat-release /etc/request-key.conf /etc/request-key.d /etc/resolv.conf /etc/rpc /etc/rpm /etc/rsyslog.conf /etc/rsyslog.d /etc/rwtab /etc/rwtab.d /etc/sasl2 /etc/securetty /etc/security /etc/selinux /etc/services /etc/sestatus.conf /etc/shadow /etc/shadow- /etc/shells /etc/skel /etc/ssh /etc/ssl /etc/statetab /etc/statetab.d /etc/subgid /etc/subuid /etc/sudo.conf /etc/sudoers /etc/sudoers.d /etc/sudo-ldap.conf /etc/sysconfig /etc/sysctl.conf /etc/sysctl.d /etc/systemd /etc/system-release /etc/system-release-cpe /etc/terminfo /etc/tmpfiles.d /etc/tuned /etc/udev /etc/vconsole.conf /etc/vimrc /etc/virc /etc/vmware-tools /etc/wgetrc /etc/wpa_supplicant /etc/X11 /etc/xdg /etc/xinetd.d /etc/yum /etc/yum.conf /etc/yum.repos.d [root@CentOS7 ~]# seq 10 1 2 3 4 5 6 7 8 9 10 [root@CentOS7 ~]# NUM=`seq 10` [root@CentOS7 ~]# echo $NUM 1 2 3 4 5 6 7 8 9 10 [root@CentOS7 ~]# echo "$NUM" 1 2 3 4 5 6 7 8 9 10 [root@CentOS7 ~]# NAMES="wang > zhang > zhao > li" [root@CentOS7 ~]# echo $NAMES wang zhang zhao li [root@CentOS7 ~]# echo "$NAMES" wang zhang zhao li [root@CentOS7 ~]#
例:变量引用
[root@CentOS7 ~]# NAME=mage [root@CentOS7 ~]# AGE=20 [root@CentOS7 ~]# echo $NAME mage [root@CentOS7 ~]# echo $AGE 20 [root@CentOS7 ~]# echo $NAME $AGE mage 20 [root@CentOS7 ~]# echo $NAME$AGE mage20 [root@CentOS7 ~]# echo $NAME_$AGE 20 [root@CentOS7 ~]# echo ${NAME}_$AGE mage_20
例:变量的间接赋值和引用
[root@CentOS7 ~]# TITLE=cto [root@CentOS7 ~]# NAME=wang [root@CentOS7 ~]# TITLE=$NAME [root@CentOS7 ~]# echo $NAME wang [root@CentOS7 ~]# echo $TITLE wang [root@CentOS7 ~]# NAME=mage [root@CentOS7 ~]# echo $NAME mage [root@CentOS7 ~]# echo $TITLE wang
例:变量追加值
[root@CentOS7 ~]# TITLE=CTO [root@CentOS7 ~]# TITLE+=:wang [root@CentOS7 ~]# echo $TITLE CTO:wang
例:利用变量实现动态命令
[root@CentOS7 ~]# CMD=hostname [root@CentOS7 ~]# $CMD CentOS7.magedu.org
显示已定义的所有变量:
set
删除变量:
unset <name>
例:
[root@CentOS7 ~]# NAME=mage [root@CentOS7 ~]# TITLE=ceo [root@CentOS7 ~]# echo $NAME $TITLE mage ceo [root@CentOS7 ~]# unset NAME TITLE [root@CentOS7 ~]# echo $NAME $TITLE [root@CentOS7 ~]#
环境变量:
#声明并赋值 export name=VALE declare -x name=VALE #或者分两步实现 name=VALUE export name
$name ${name}
env printenv export declare -x
unset name
只读变量:只能声明定义,但后续不能修改和删除,及常量,当前shell进程退出后,只读变量随之删除
readonly name declare -r name
readonly [-p] declare -r
在bash shell中内置的变量,在脚本代码中调用通过命令行传递给脚本的参数
清空所有位置变量
set --
进程执行后,将使用变量$?保存状态码的相关数字,不同的值反应成功或失败,$?取值范围0-255
$?的值为0 #代表成功 $?的值为1-255 #代表失败
用户可以在脚本中使用以下命令自定义退出状态码
exit [n] #n取值范围:0-255
注意:
展开命令行顺序
把命令行分成单个命令词 展开别名 展开大括号的声明{} 展开波浪符声明 ~ 命令替换$()和`` 再次把命令行分为命令词 展开文件通配*、?、[abc]等等 准备I/O重导向<、> 运行命令
防止扩展
反斜线(\)会使随后的字符按原意解释
加引号来防止扩展
单引号('')防止所有扩展 双引号("")也可防止扩展,但是以下情况例外:$
变量扩展
``:反引号,命令替换 \:反斜线,禁止单个字符扩展 !:叹号,历史命令替换
-u 在扩展一个没有设置的变量时,显示错误信息,等同set -o nounset
-e 如果一个命令返回一个非0退出状态值(失败)就退出,等同set -o errexit
-o option 显示,打开或关闭选项
显示选项:set -o
打开选项:set -o 选项
关闭选项:set +o 选项
-x 当执行命令时,打印命令及其参数,类似bash -x