天涼好個秋.

shell遍历字符串中的字符

856 阅 0 评 问题&解决方案

需求

遍历字符串中的每一个字符。

实现

str="abcdefg"
for i in `seq ${#str}`  
do  
    echo ${str:$((i-1)):1} 
done     

输出结果

a
b
c
d
e
f
g
root@Hiwifi:~#

附录

循环遍历CSDN
shell的字符串操作

bug

发现CSDN博主的bug,已经提交给作者。

EOF