定义一个变量aaa
1 [root@rhel7-1 test]# aaa="I am a chairman" 2 [root@rhel7-1 test]# echo $aaa 3 I am a chairman 4 [root@rhel7-1 test]# echo ${aaa} 5 I am a chairman
实现计算的四种方法:
1 方法一: 2 [root@rhel7-1 test]# echo ${#aaa} 3 15 4 方法二: 5 [root@rhel7-1 test]# expr length "$aaa" 6 15 7 方法三: 8 [root@rhel7-1 test]# echo ${aaa} | wc -L 9 15 10 方法四: 11 [root@rhel7-1 test]# echo ${aaa} | awk '{print length ($0)}' 12 15
简单shell小例子:利用for循环打印字符小于7的单词
1 [root@rhel7-1 test]# cat word_length.sh 2 #!/bin/bash 3 #打印字符小于7的单词 4 5 aaa="Dont forget a persons greatest emotional need is to feel appreciated" 6 for i in $aaa 7 do 8 if [ `expr length $i` -le 6 ] 9 then 10 echo $i 11 fi 12 done