首页 / 更多教程 / Shell中的 IFS
Shell中的 IFS
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Shell中的 IFS,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2349字,纯文字阅读大概需要4分钟。
内容图文
一、IFS 介绍
Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符。完整定义是The shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.
当 shell 处理"命令替换"和"参数替换"时,shell 根据 IFS 的值,默认是 space, tab, newline 来拆解读入的变量,然后对特殊字符进行处理,最后重新组合赋值给该变量。
对于IFS的使用,唯一的途径,就是了解shell的执行顺序。在《shell脚本学习指南》第7章,179页,有个图,说的很清晰明了。在命令行上,对单引号字符串、双引号字符串、不带引号字符串的引用如何处理都讲的很清晰,以及IFS分隔字符串的时机。
总体上记住一点,双引号和单引号字符串引用可以屏蔽对命令参数使用IFS分割。
如上图所示,shell命令被解析执行的顺序。在第①步和第⑨步,都会进行一个 split 操作。在第①步中,我们在命令行上输入命令,然后shell首先会把它们以IFS作为分隔符split为token, 然后到了第⑨步中,参数扩展和命令替换后的结果也会被以IFS为分隔符,split为word。
以下是值得注意的地方:
-
IFS is the space, tab, and newline characters by default,连续多个空白被看做一个处理
-
"$*" 使用IFS中的第一个字符作为分隔符,把参数连接
-
awk中的FS(域分隔符)也和IFS有类似的用法和作用
二、IFS 简单实例
1、查看IFS的值
[root@localhost ~]# set | grep IFS IFS=$‘ \t\n‘ [root@localhost ~]# echo -n "$IFS" | od -ab 0000000 sp ht nl 040 011 012 0000003
2、"$@" 和 "$*" 异同
@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
"$@" "$*" 只有当用双引号quoting时, 它们才有所不同。 $@ $* 两种结果是相同的。
本文出自 “Share your knowledge” 博客,请务必保留此出处http://skypegnu1.blog.51cto.com/8991766/1543319
原文:http://skypegnu1.blog.51cto.com/8991766/1543319
内容总结
以上是互联网集市为您收集整理的Shell中的 IFS全部内容,希望文章能够帮你解决Shell中的 IFS所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。