what's the meaning of IFS
2016-05-25 21:47
447 查看
IFS stands for “internal field separator”. It is used by the shell to determine how to do word splitting, i. e. how to recognize word boundaries.
Try this in a shell like bash (other shells may handle this differently, for example zsh):
The default value for IFS consists of whitespace characters (to be precise: space, tab and newline). Each character can be a word boundary. So, with the default value of IFS, the loop above will print:
Word: foo:bar
Word: baz
Word: rab
In other words, the shell thinks that whitespace is a word boundary.
Now, try setting IFS=: before execting the loop. This time, the result is:
Word: foo
Word: bar baz rab
Now, the shell splits mystring into words as well – but now, it only treats a colon as the word boundary.
The first character of IFS is special: It is used to delimit words in the output when using the special $* variable (example taken from the Advanced Bash Scripting Guide, where you can also find more information on special variables like that one):
Note that in both examples, the shell will still treat all of the characters :, - and ; as word boundaries. The only thing that changes is the behaviour of $*.
Another important thing to know is how so-called “IFS whitespace” is treated. Basically, as soon as IFS includes whitespace characters, leading and trailing whitespace is stripped from the string to be split before processing it and a sequence of consecutive whitespace characters delimits fields as well. However, this only applies to those whitespace characters which are actually present in IFS.
For example, let’s look at the string “a:b:: c d ” (trailing space and two space characters between c and d).
Note how multiple, consecutive whitespace characters delimit two fields in the second example, while multiple, consecutive colons don’t (since they are not whitespace characters).
As for IFS=$’\n’, that is bash-specific syntax. Quoting the bash manpage:
\n is the escape sequence for a newline, so IFS ends up being set to a single newline character.
Try this in a shell like bash (other shells may handle this differently, for example zsh):
mystring="foo:bar baz rab" for word in $mystring; do echo "Word: $word" done
The default value for IFS consists of whitespace characters (to be precise: space, tab and newline). Each character can be a word boundary. So, with the default value of IFS, the loop above will print:
Word: foo:bar
Word: baz
Word: rab
In other words, the shell thinks that whitespace is a word boundary.
Now, try setting IFS=: before execting the loop. This time, the result is:
Word: foo
Word: bar baz rab
Now, the shell splits mystring into words as well – but now, it only treats a colon as the word boundary.
The first character of IFS is special: It is used to delimit words in the output when using the special $* variable (example taken from the Advanced Bash Scripting Guide, where you can also find more information on special variables like that one):
$ bash -c 'set w x y z; IFS=":-;"; echo "$*"' w:x:y:z Compare to: $ bash -c 'set w x y z; IFS="-:;"; echo "$*"' w-x-y-z
Note that in both examples, the shell will still treat all of the characters :, - and ; as word boundaries. The only thing that changes is the behaviour of $*.
Another important thing to know is how so-called “IFS whitespace” is treated. Basically, as soon as IFS includes whitespace characters, leading and trailing whitespace is stripped from the string to be split before processing it and a sequence of consecutive whitespace characters delimits fields as well. However, this only applies to those whitespace characters which are actually present in IFS.
For example, let’s look at the string “a:b:: c d ” (trailing space and two space characters between c and d).
With IFS=: it would be split into four fields: "a", "b", "" (empty string) and " c d " (again, two spaces between c and d). Note the leading and trailing whitespace in the last field. With IFS= :, it would be split into five fields: "a", "b", "" (empty string), "c" and "d". No leading and trailing whitespace anywhere.
Note how multiple, consecutive whitespace characters delimit two fields in the second example, while multiple, consecutive colons don’t (since they are not whitespace characters).
As for IFS=$’\n’, that is bash-specific syntax. Quoting the bash manpage:
Words of the form $'string' are treated specially. The word expands to "string", with backslash-escaped characters replaced as specified by the ANSI C standard.
\n is the escape sequence for a newline, so IFS ends up being set to a single newline character.
相关文章推荐
- Access Denied: Specified user is not is not a member of TelnetClients group解决方法
- Hadoop2.7.2之集群搭建(高可用)
- 放球游戏(51nod 1418)模拟
- MySQL各种存储引擎的对比
- source insight 添加 python 支持
- C++走向远洋——53(项目一1、分数类的重载、加减乘除、比较)
- javaSE 模拟Spring框架的Ioc
- 多重背包(动态规划)
- zxing私人定制之一 初试扫码
- malloc与free函数用法
- CodeForces 590A
- Git and GitHub
- HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)
- Java系统工具jps原理解析
- Flume-ng的原理和使用
- poj 1845 Sumdiv
- 2016/05/25 get和post的区别
- python简单操作excel
- 6.python 相对导入
- N层交换