lang="javascript is good"
echo ${lang#java*}
echo ${lang#java}
echo ${lang#[^b-c]}
echo ${lang#[^j-z]}
# 输出
# script is good
# script is good
# avascript is good
# javascript is good
lang="javascript is good, good study"
echo ${lang##*,}
echo ${lang##java}
# 输出
# good study
# script is good, good study
${str%substr}:从str结尾删除最短匹配$substr的子串
lang="javascript is good, good,study"
good="javascript is good, good study"
echo ${lang%,*}
echo ${good%,*}
# 输出
# javascript is good, good
# javascript is good
${str%%substr}:从str结尾删除最长匹配$substr的子串
lang="javascript is good, good,study"
good="javascript is good, good study"
echo ${lang%%,*}
echo ${good%%,*}
# 输出
# javascript is good
# javascript is good
lang="java is good, javascript is good"
lang2="python"
bast="the best"
echo ${lang/java/$lang2}
echo ${lang/good/$bast}
# 输出
# python is good, javascript is good
# java is the best, javascript is good
lang="java is good, javascript is good"
lang2="python"
bast="the best"
echo ${lang//java/$lang2}
echo ${lang//good/$bast}
# 输出
# python is good, pythonscript is good
# java is the best, javascript is the best
lang="java is good, javascript is good"
bast="the bast"
echo ${lang/%good/$bast}
# 输出
# java is good, javascript is the best
使用实例
列出当前文件夹下所有文件含有的后缀名
function hasItem() {
arr=$1;
item=$2;
for ele in ${arr[@]}
do
if [[ $item = $ele ]]; then
return 0
fi
done
return 1
}
arr=()
for file in `ls`
do
ext=${file#*.}
echo ${arr[@]}
hasItem $arr $ext
if [ $? -eq 1 ]; then
length=${#arr[*]}
if [ $length -eq 0 ]; then
arr=($ext)
else
arr=("${arr[@]}" "${ext}")
fi
else
arr=$arr
fi
done
for name in ${arr[@]}
do
echo $name
done
在Shell编程快速入门指南一文中已经简单介绍了字符串的变量命名、截取、获取长度等操作,但通常我们对字符串的操作的需求远远不止这些,Shell本身一起已经内置了一些对字符串的操作。
判断和读取字符串
c=${d-$b} e=${a-$b}
echo $c echo $e
输出
win
linux
*${var+other} 和 ${var:+other}:如果var声明了,那么其值就是$other,否则就是null字符串(echo打印为一个空行)
${var?ERR_MSG} 和 ${var:?ERR_MSG}:如果var没有被声明,就会打印ERR_MSG(也就是说会打印出错误信息,用在调试中会非常有用)
*${!varprefix} 和 ${!varprefix@}**:匹配之前所有以varprefix开头进行声明的变量,这是一个数组
字符串长度获取、替换、截取
${#str}:获取长度
${str:position:length}:从position位置开始截取长度为length的子串,其中length可以省略,省略则是截取到最后
${str#substr}:从str开头删除最短匹配$substr的子串,匹配都是从开头匹配的,开头匹配不上则不删除
${str#substr}:从str开头删除最长匹配$substr的子串,匹配都是从开头匹配的,开头匹配不上则不删除
${str%substr}:从str结尾删除最短匹配$substr的子串
${str%%substr}:从str结尾删除最长匹配$substr的子串
${str/substr/replacement}:使用$replacement来替代第一个匹配的substr子串
${str//substr/replacement}:使用$replacement来替代所有匹配的substr子串
${str/#substr/replacement}:如果$str的前缀匹配$substr则使用$replacement替代
${str/%substr/replacement}:如果$str的后缀缀匹配$substr则使用$replacement替代
使用实例
列出当前文件夹下所有文件含有的后缀名