| 
 1 
2 
3 
4 
5 
6 
7 
8 
 | 
#!/bin/shmsg="welcome to www groad net"for item in $msgdo    echo "Item: $item"done | 
上 面用一个 for 循环遍历了变量 msg 里的所有项。 msg 变量里存储的各个单词都是用空格分开的,而 for 能依次取出这些单词,正是依靠 IFS 这个变量作为分隔符。如果将 msg 变量改为 CSV (comma separaed values 逗号分隔值)格式,那么按照默认的 IFS 值就无法解析出各个单词,如:# sh temp.sh Item: welcome Item: to Item: www Item: groad Item: net
这样,整个字符串就当成一个 item 被获取了。sh temp.sh Item: welcome,to,www,groad,net
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
#!/bin/shdata="welcome,to,www,groad,net"IFSBAK=$IFS     #备份原来的值IFS=,for item in $datado    echo Item: $itemdoneIFS=$IFSBAK     #还原 | 
# sh tmp.sh Item: welcome Item: to Item: www Item: groad Item: net
原文:http://www.cnblogs.com/feiyun8616/p/6074136.html