变量替换_转义_命令替换-2

转义

[root@localhost shell_study]# echo "a\nb"
a\nb
[root@localhost shell_study]# echo -e "a\nb"
a
b

变量替换

形式 说明
${var} 变量本来的值
${var:-word} 如果变量 var 为空或已被删除(unset),那么返回 word,但不改变 var 的值。
${var:=word} 如果变量 var 为空或已被删除(unset),那么返回 word,并将 var 的值设置为 word。
${var:?message} 如果变量 var 为空或已被删除(unset),那么将消息 message 送到标准错误输出,可以用来检测变量 var 是否可以被正常赋值。若此替换出现在Shell脚本中,那么脚本将停止运行。
${var:+word} 如果变量 var 被定义,那么返回 word,但不改变 var 的值。

示例:

#!/bin/bash
unset a
echo "a-:${a:- aaa}"
echo "a:${a}"
            
unset b     
echo "b+:${b:+ bbb}"
echo "b:${b}" 
           
unset c 
echo "c=:${c:= ccc}"
echo "c:${c}" 
[root@localhost shell_study]# ./test3.sh 
a-: aaa
a:
b+:
b:
c=: ccc
c: ccc

命令替换

在赋值语句中,让命令还是命令

#!/bin/bash
DATE=`date` #该符号在ESC下
echo "Date is $DATE"
[root@localhost shell_study]# ./test4.sh 
Date is 2017年 01月 10日 星期二 00:44:32 CST

你可能感兴趣的:(变量替换_转义_命令替换-2)