shell变量替换总结

2020-02-18  本文已影响0人  AntKing

1: ${变量#匹配规则} 从头开始匹配,最短删除

variable_1="i love you, do you love me"
var=${variable_1#*ov}
echo $var

输出结果 e you, do you love me

2: ${变量##匹配规则} 从头开始匹配,最长删除

variable_1="i love you, do you love me"
var=${variable_1##*ov}
echo $var

输出结果e me

3: ${变量%匹配规则} 从尾开始匹配,最短删除

variable_1="i love you,do you love me"
var=${variable_1%ov*}
echo $var

输出结果 i love you,do you l

4: ${变量%%匹配规则} 从尾开始匹配,最长删除

variable_1="i love you,do you love me"
var=${variable_1%%ov*}
echo $var

输出结果 i l

5: ${变量/旧字符串/新字符串} 用新字符串替换变零中第一个旧字符串

var5="/usr/local/Cellar/dart/2.5.2/bin:/Users/lihe/Downloads/flutter/bin"
var6=${var5/bin/BIN}
echo ${var6}

输出结果
/usr/local/Cellar/dart/2.5.2/BIN:/Users/lihe/Downloads/flutter/bin

5: ${变量//旧字符串/新字符串} 用新字符串替换变量中所有旧字符串

var5="/usr/local/Cellar/dart/2.5.2/bin:/Users/lihe/Downloads/flutter/bin"
var6=${var5//bin/BIN}
echo ${var6}

输出结果
/usr/local/Cellar/dart/2.5.2/BIN:/Users/lihe/Downloads/flutter/BIN

变量测试

image.png
上一篇下一篇

猜你喜欢

热点阅读