对于IFS的一点总结

IFS是bash的内部变量,称为内部域分隔符.这个变量用来决定Bash在解释字符串时如何识别域,或者单词边界.

 

下面是转载的文章:

(1)什么是IFS ?
IFS是bash的内部变量,称为内部域分隔符.这个变量用来决定Bash在解释字符串时如何识别域,或者单词边界.

(2)如何查看当前的IFS值?

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"


由于IFS默认为空白(空格,tab和新行),所以使用以上的命令似乎看不到字符。没关系,你可以用od命令看16进制,或是2进制值:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004


注意:$*使用$IFS 中的第一个字符,比如:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w x y z



(3)如何修改IFS值?
普通的赋值命令即可:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
:
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 0a
0000002
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w:x:y:z



(4)实验:$*使用$IFS 中的第一个字符

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS="\\:;"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
\:;
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 5c 3a 3b 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w\x\y\z
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":;\\"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
:;\
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 3b 5c 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w:x:y:z


(5)备份IFS

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ OLDIFS="$IFS" 
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$OLDIFS" | od -t x1
0000000 20 09 0a 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":" 
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 0a
0000002
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS="$OLDIFS" 
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004

 

posted @ 2010-10-11 14:20  卜海清  阅读(177)  评论(0)    收藏  举报