注意:${test##*/},${test%/*} 分别是得到文件名,或者目录地址最简单方法。
4.字符串替换
[chengmo@localhost ~]$ test='c:/windows/boot.ini'
[chengmo@localhost ~]$ echo ${test///}
c:windows/boot.ini
[chengmo@localhost ~]$ echo ${test////}
c:windowsboot.ini
${变量/查找/替换值}一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”/”表示。
三、性能比较
在shell中,通过awk,sed,expr等都可以实现,字符串上述操作。下面我们进行性能比较。
[chengmo@localhost ~]$test='c:/windows/boot.ini'
[chengmo@localhost ~]$ time for i in $(seq 10000);doa=${#test};done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[chengmo@localhost ~]$ time for i in $(seq 10000);do a=$(expr length$test);done;
real 0m9.734s
user 0m1.628s
速度相差上百倍,调用外部命令处理,与内置操作符性能相差非常大。在shell编程中,尽量用内置操作符或者函数完成。使用awk,sed类似会出现这样结果。
Yorking Alan










