linux中的echo%3e文件,Linux学习笔记-shell脚本中${}的使用方法

${}

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

# 举例说明

[[email protected] ~]$ file=/dir1/dir2/dir3/my.file.txt

# 变量替换

[[email protected] ~]$ echo ${file}

/dir1/dir2/dir3/my.file.txt

# 拿掉第一条 / 及其左边的字符串

[[email protected] ~]$ var1=${file#*/}

[[email protected] ~]$ echo $var1

dir1/dir2/dir3/my.file.txt

# 从字符串的左边开始匹配,并去除其左侧字符串

[[email protected] ~]$ var2=${file#*dir1/}

[[email protected] ~]$ echo $var2

dir2/dir3/my.file.txt

# 拿掉最后一条 / 及其左边的字符串

[[email protected] ~]$ var1=${file##*/}

[[email protected] ~]$ echo $var1

my.file.txt

# 从字符串的右边开始匹配,并去除其左侧字符串

[[email protected] ~]$ var2=${file##*dir*/}

[[email protected] ~]$ echo $var2

my.file.txt

# 拿掉最后一条 / 及其右边的字符串

[[email protected] sh_code]$ var1=${file%/*}

[[email protected] sh_code]$ echo $var1

/dir1/dir2/dir3

# 从字符串右侧开始匹配,并去除其右侧内容

[[email protected] sh_code]$ var2=${file%/dir*}

[[email protected] sh_code]$ echo $var2

/dir1/dir2

# 拿掉第一条 / 及其右边的字符串

[[email protected] sh_code]$ var1=${file%%/*}

[[email protected] sh_code]$ echo $var1

# 从字符串左侧开始匹配,并去除其右侧内容

[[email protected] sh_code]$ var2=${file%%/dir*}

[[email protected] sh_code]$ echo $var2

[[email protected] sh_code]$ var3=${file%%/dir2*}

[[email protected] sh_code]$ echo $var3

/dir1

## 记忆的方法为:

## "#"是去掉左边(在键盘上#在$之左边)

## "%"是去掉右边(在键盘上% 在$之右边)

## 单一符号是最小匹配﹔两个符号是最大匹配。

# ${file:0:5}:提取最左边的5个字节

[[email protected] sh_code]$ var1=${file:0:5}

[[email protected] sh_code]$ echo $var1

/dir1

# ${file:5:5}:提取第5个字节右边的连续5个字节

[[email protected] sh_code]$ var2=${file:5:5}

[[email protected] sh_code]$ echo $var2

/dir2

# 从字符串右侧截取5个字符,并提取截取字符串左侧2字符

[[email protected] sh_code]$ var3=${file:0-5:2}

[[email protected] sh_code]$ echo $var3

e.

# 从字符串右侧截取5个字符,并提取截取字符串第2个字符右边的2个字符

[[email protected] sh_code]$ var3=${file:2-5:2}

[[email protected] sh_code]$ echo $var3

tx

# 以对变量值里的字符串作替换

## 将第一个dir提换为path

[[email protected] sh_code]$ var1=${file/dir/path}

[[email protected] sh_code]$ echo $var1

/path1/dir2/dir3/my.file.txt

## 将全部dir提换为path

[[email protected] sh_code]$ var2=${file//dir/path}

[[email protected] sh_code]$ echo $var2

/path1/path2/path3/my.file.txt

# 利用${}还可针对不同的变量状态赋值(没设定、空值、非空值)

## ${file-my.file.txt}:假如$file没有设定,则使用 my.file.txt 作传回值。(空值及非空值时不作处理)

[[email protected] sh_code]$ var1=${file1-my.file1.txt}

[[email protected] sh_code]$ echo $var1

my.file1.txt

[[email protected] sh_code]$ var2=${file-my.file1.txt}

[[email protected] sh_code]$ echo $var2

/dir1/dir2/dir3/my.file.txt

## ${file:-my.file.txt}:假如$file 没有设定或为空值,则使用my.file.txt作传回值。 (非空值时不作处理)

[[email protected] sh_code]$ file1=""

[[email protected] sh_code]$ echo $file1

[[email protected] sh_code]$ var1=${file1:-my.file1.txt}

[[email protected] sh_code]$ echo $file1

[[email protected] sh_code]$ echo $var1

my.file1.txt

## ${file+my.file.txt}:不管$file为何值,均使用 my.file.txt作传回值 。

[[email protected] sh_code]$ var1=${file+my.file1.txt}

[[email protected] sh_code]$ echo $var1

my.file1.txt

## ${file:+my.file.txt}:若$file为非空值,则使用 my.file.txt作传回值。(没设定及空值时不作处理)

[[email protected] sh_code]$ var1=${file:+my.file1.txt}

[[email protected] sh_code]$ echo $var1

my.file1.txt

[[email protected] sh_code]$ var1=${file2:+my.file1.txt}

[[email protected] sh_code]$ echo $var1

## ${file=my.file.txt}:若$file没设定,则使用 my.file.txt作传回值,同时将$file赋值为my.file.txt。 (空值及非空值时不作处理)

[[email protected] sh_code]$ var1=${file2=my.file1.txt}

[[email protected] sh_code]$ echo $var1

my.file1.txt

[[email protected] sh_code]$ echo $file2

my.file1.txt

## ${file:=my.file.txt}:若$file 没设定或为空值,则使用my.file.txt作传回值,同时将 $file赋值为my.file.txt。(非空值时不作处理)

[[email protected] sh_code]$ var1=${file3:=my.file3.txt}

[[email protected] sh_code]$ echo $var1

my.file3.txt

[[email protected] sh_code]$ echo $file3

my.file3.txt

## ${file?my.file.txt}:若$file没设定,则将 my.file.txt输出至STDERR。(空值及非空值时不作处理)

[[email protected] sh_code]$ var1=${file5?my.file5.txt}

-bash: file5: my.file5.txt

${file:?my.file.txt}:若$file没设定或为空值,则将 my.file.txt输出至STDERR。(非空值时不作处理)

[[email protected] sh_code]$ file6=""

[[email protected] sh_code]$ var1=${file6:?my.file6.txt}

-bash: file6: my.file6.txt

${#var} 可计算出变量值的长度

[[email protected] sh_code]$ var=abcdef

[[email protected] sh_code]$ len=${#var}

[[email protected] sh_code]$ echo $len

6

原文:https://www.cnblogs.com/dataanaly/p/12864656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值