- str="we are not the man who would just accept everything!"
2022 $ echo ${#str}
52 - str="we are not the man who would just accept everything! "
2020 $ echo ${#str}
53
对比发现string的末尾空格是会被算长度的
2008 $ echo ${str:2}
are not the man who would just accept everything!
2013 $ echo ${str:2}|wc -c
502011 $ echo ${str:3}
are not the man who would just accept everything!
2012 $ echo ${str:3}|wc -c
50
对比发现字符串截取时第一位是空格的话echo默认去掉了空格。
- 2014 $ str="we are not the man who would just accept everything! "
[root@mc-adn-deploy-172-17-0-17@~@17:02:23]
2015 $ echo ${str:2}|wc -c
50
对比发现字符串末尾的空格在字符串被截取时也被移除了,所以长度和末尾没有空格时一样——这时因为用了wc -c ?
是的。请看:
2028 $ str2=${str:2}
2029 $ echo ${#str2}
54
此处将截取的字符串内容保存再算长度,得到的就是包含空格的长度。
echo是很好的去掉字符串开始和末尾的空格的一种方式!