Last updated 3 years ago
Was this helpful?
在shell编程中,经常需要移除字符串最后一些字符,在bash中,有一个非常方便的方法:
#!/bin/bash v="some string.rtf" v2=${v::-4} echo "$v --> $v2"
不过,这个方法对bash版本有一定要求,需要 bash 4+
更为通用的方法结合使用 rev 和 cut ,原理是先通过 rev 反转字符串,然后通过 cut 将反转后的字符串开头n个字符移除,然后再次反转字符串
rev
cut
echo "hello world" | rev | cut -c5- | rev
返回结果就是
hello w