${value:-word}
If the variable name exists and is not NULL, the value of the variable is returned, otherwise the word string is returned.
${value:=word}
If the variable name exists and is not NULL, the value of the variable is returned, otherwise, the value of this variable is word and its value is returned.
${value:?message}
If the variable name exists and is not NULL, the value of the variable is returned, otherwise the variable name is displayed, and the current command or shell is exited.
Example: ${value:? " Notdefined "} If value is undefined, the-bash:value:not defined is displayed and exited.
${value:+word}
If the variable name exists and is not NULL, Word is returned, otherwise null is returned.
Example: ${value:+word} if value is already defined, return Word (that is, true)
1, ${value:-word}
When the variable is undefined or the value is empty, the return value is the word content, otherwise the value of the variable is returned
[Email protected] ~]# Result=${cmz:-unset}#当cmz没有定义合作和是空会返回UNSET
[Email protected] ~]# echo $result
UNSET
[Email protected] ~]# echo $CMZ
==> here is empty.
Conclusion: When CMZ has no content, it returns to unset.
[Email protected] ~]# cmz= "Caimengzhi"
[Email protected] ~]# Result=${cmz:-unset} #之前必须声明cmz变量
[Email protected] ~]# echo $result
Caimengzhi
Conclusion: When CMZ has content, it returns the contents of CMZ variables.
This variable function solves the work string problem, and the variable is undefined.
2, ${value:=word}
[[email protected] ~]# Result=${test:=unset}
[Email protected] ~]# echo $result
UNSET
[Email protected] ~]# echo $test
UNSET
3, ${value:? " Not defined "}
[Email protected] ~]# RESULT=${LMT:? Not defined}
-bash:lmt:not defined
4, ${value-word} remove Colon
That is, when httpd does not exist, it assigns-the/usr/sbin/httpd of the back to the httpd in front of the equal sign.
To manipulate the path of a variable, it is best to first determine whether the path is non-null, especially the delete operation.
Path=/server/backup
Find ${path:=/tmp/}-name "*.tar.gz"-type F | Xargs rrm-f
RM-RF ${path}
It's important.
[Email protected] shell_scrpit]# vim d.sh
[Email protected] shell_scrpit]# sh-x d.sh
+ find/tmp/-name ' *.tar.gz '-type f
+ Xargs Rrm-f
Xargs:rrm:No such file or directory
+ RM-RF
[email protected] shell_scrpit]# cat d.sh
#path =/server/backup
Find ${path:=/tmp/}-name "*.tar.gz"-type F | Xargs rrm-f
#此时path没有定义 path will become/tmp/
RM-RF ${path}
Practice and production practice of shell variable substitution technology