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