免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1081 | 回复: 0
打印 上一主题 下一主题

对于IFS的一点总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-21 13:10 |只看该作者 |倒序浏览
在编写shell脚本的时候,时常会碰到IFS变量,但我对于具体的使用和设置常常比较模糊,今天花了一点时间googel和实验了一下,总结如下:
(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
参考资料:
《[精彩] 关于IFS的疑问》
         《ABS 3.7.2中文版》翻译:杨春敏 黄毅



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/34474/showart_2002264.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP