${}


除了做${var}变量替换,还有什么功能呢?

# 举例说明
[why@jwyx ~]$ file=/dir1/dir2/dir3/my.file.txt
# 变量替换
[why@jwyx ~]$ echo ${file}
/dir1/dir2/dir3/my.file.txt

# 拿掉第一条 / 及其左边的字符串
[why@jwyx ~]$ var1=${file#*/}
[why@jwyx ~]$ echo $var1
dir1/dir2/dir3/my.file.txt
# 从字符串的左边开始匹配,并去除其左侧字符串
[why@jwyx ~]$ var2=${file#*dir1/}
[why@jwyx ~]$ echo $var2
dir2/dir3/my.file.txt

# 拿掉最后一条 / 及其左边的字符串
[why@jwyx ~]$ var1=${file##*/}
[why@jwyx ~]$ echo $var1
my.file.txt
# 从字符串的右边开始匹配,并去除其左侧字符串
[why@jwyx ~]$ var2=${file##*dir*/}
[why@jwyx ~]$ echo $var2
my.file.txt

# 拿掉最后一条 / 及其右边的字符串
[why@jwyx sh_code]$ var1=${file%/*}
[why@jwyx sh_code]$ echo $var1
/dir1/dir2/dir3
# 从字符串右侧开始匹配,并去除其右侧内容
[why@jwyx sh_code]$ var2=${file%/dir*}
[why@jwyx sh_code]$ echo $var2
/dir1/dir2
# 拿掉第一条 / 及其右边的字符串
[why@jwyx sh_code]$ var1=${file%%/*}
[why@jwyx sh_code]$ echo $var1

# 从字符串左侧开始匹配,并去除其右侧内容
[why@jwyx sh_code]$ var2=${file%%/dir*}
[why@jwyx sh_code]$ echo $var2

[why@jwyx sh_code]$ var3=${file%%/dir2*}
[why@jwyx sh_code]$ echo $var3
/dir1

## 记忆的方法为:   
##   "#"是去掉左边(在键盘上#在$之左边)   
##   "%"是去掉右边(在键盘上% 在$之右边)   
##   单一符号是最小匹配﹔两个符号是最大匹配。


# ${file:0:5}:提取最左边的5个字节 
[why@jwyx sh_code]$ var1=${file:0:5}
[why@jwyx sh_code]$ echo $var1
/dir1
# ${file:5:5}:提取第5个字节右边的连续5个字节
[why@jwyx sh_code]$ var2=${file:5:5}
[why@jwyx sh_code]$ echo $var2
/dir2
# 从字符串右侧截取5个字符,并提取截取字符串左侧2字符
[why@jwyx sh_code]$ var3=${file:0-5:2}
[why@jwyx sh_code]$ echo $var3
e.
# 从字符串右侧截取5个字符,并提取截取字符串第2个字符右边的2个字符
[why@jwyx sh_code]$ var3=${file:2-5:2}
[why@jwyx sh_code]$ echo $var3
tx


# 以对变量值里的字符串作替换
## 将第一个dir提换为path
[why@jwyx sh_code]$ var1=${file/dir/path}
[why@jwyx sh_code]$ echo $var1
/path1/dir2/dir3/my.file.txt
## 将全部dir提换为path
[why@jwyx sh_code]$ var2=${file//dir/path}
[why@jwyx sh_code]$ echo $var2
/path1/path2/path3/my.file.txt


# 利用${}还可针对不同的变量状态赋值(没设定、空值、非空值)
## ${file-my.file.txt}:假如$file没有设定,则使用  my.file.txt  作传回值。(空值及非空值时不作处理) 
[why@jwyx sh_code]$ var1=${file1-my.file1.txt}
[why@jwyx sh_code]$ echo $var1
my.file1.txt
[why@jwyx sh_code]$ var2=${file-my.file1.txt}
[why@jwyx sh_code]$ echo $var2
/dir1/dir2/dir3/my.file.txt
## ${file:-my.file.txt}:假如$file 没有设定或为空值,则使用my.file.txt作传回值。  (非空值时不作处理)
[why@jwyx sh_code]$ file1=""
[why@jwyx sh_code]$ echo $file1

[why@jwyx sh_code]$ var1=${file1:-my.file1.txt}
[why@jwyx sh_code]$ echo $file1

[why@jwyx sh_code]$ echo $var1
my.file1.txt
## ${file+my.file.txt}:不管$file为何值,均使用 my.file.txt作传回值 。 
[why@jwyx sh_code]$ var1=${file+my.file1.txt}
[why@jwyx sh_code]$ echo $var1
my.file1.txt
## ${file:+my.file.txt}:若$file为非空值,则使用  my.file.txt作传回值。(没设定及空值时不作处理) 
[why@jwyx sh_code]$ var1=${file:+my.file1.txt}
[why@jwyx sh_code]$ echo $var1
my.file1.txt
[why@jwyx sh_code]$ var1=${file2:+my.file1.txt}
[why@jwyx sh_code]$ echo $var1

## ${file=my.file.txt}:若$file没设定,则使用  my.file.txt作传回值,同时将$file赋值为my.file.txt。 (空值及非空值时不作处理)
[why@jwyx sh_code]$ var1=${file2=my.file1.txt}
[why@jwyx sh_code]$ echo $var1
my.file1.txt
[why@jwyx sh_code]$ echo $file2
my.file1.txt
## ${file:=my.file.txt}:若$file 没设定或为空值,则使用my.file.txt作传回值,同时将 $file赋值为my.file.txt。(非空值时不作处理)
[why@jwyx sh_code]$ var1=${file3:=my.file3.txt}
[why@jwyx sh_code]$ echo $var1
my.file3.txt
[why@jwyx sh_code]$ echo $file3
my.file3.txt
## ${file?my.file.txt}:若$file没设定,则将  my.file.txt输出至STDERR。(空值及非空值时不作处理)
[why@jwyx sh_code]$ var1=${file5?my.file5.txt}
-bash: file5: my.file5.txt
${file:?my.file.txt}:若$file没设定或为空值,则将  my.file.txt输出至STDERR。(非空值时不作处理)
[why@jwyx sh_code]$ file6=""
[why@jwyx sh_code]$ var1=${file6:?my.file6.txt}
-bash: file6: my.file6.txt

${#var}  可计算出变量值的长度
[why@jwyx sh_code]$ var=abcdef
[why@jwyx sh_code]$ len=${#var}
[why@jwyx sh_code]$ echo $len
6

 posted on 2020-05-10 19:20  WarningMessage  阅读(657)  评论(0编辑  收藏  举报