在linux中IFS是分隔符的意思,linux中变量分两种,全局变量env和局部变量set,set中包含了env的全部变量,我们查看IFS的值发现env | grep IFS为空,而set | grep IFS有值,说明IFS是局部变量,
gl@gl:~$ set | grep ^IFS
IFS=$' \t\n'
从上面可以看出,IFS是已空格、制表符、换行符来进行分隔的。
脚本1:
#!/bin/bash
IFS=","
str="a,b,c,d,e,f,g"
echo $str
for i in $str
do
echo $i
done
运行结果:
gl@gl:~$ ./tmp
a b c d e f g
a
b
c
d
e
f
g
对于非特殊的分隔符,以下三种是等价的
IFS=","
IFS=$","
IFS=$','
IFS默认值是空格、制表符和换行符,此处以换行符\n来讨论,IFS='\n',IFS=$"\n",IFS=$'\n',第一个和第二个是等价的,就是普通字符反斜杠和n,第三个是一个先转换为了回车符NL,在屏幕上直接就回车了。
对于特殊字符
gl@gl:~$ echo "\n"
\n
gl@gl:~$ echo $"\n"
\n
gl@gl:~$ echo $'\n'
gl@gl:~$
脚本2:
gl@gl:~/gl/shell/input$ cat text
#!/bin/bash
IFS="\n"
#IFS=$"\n"
#IFS=$'\n'
str="a\nb\nc\nd\\n"
echo $str
for i in $str
do
echo $i
done
echo "===================="
str2="anbncndn"
echo $str2
for j in $str2
do
echo $j
done
结果是:
gl@gl:~$ ./text
a b c d
a
b
c
d
====================
a b c d
a
b
c
d
调试为:
gl@gl:~$ bash -x text
+ IFS='\n'
+ str='a\nb\nc\nd\n'
+ echo a '' b '' c '' d ''
a b c d
+ for i in $str
+ echo a
a
+ for i in $str
+ echo
+ for i in $str
+ echo b
b
+ for i in $str
+ echo
+ for i in $str
+ echo c
c
+ for i in $str
+ echo
+ for i in $str
+ echo d
d
+ for i in $str
+ echo
+ echo ====================
====================
+ str2=anbncndn
+ echo a b c d
a b c d
+ for j in $str2
+ echo a
a
+ for j in $str2
+ echo b
b
+ for j in $str2
+ echo c
c
+ for j in $str2
+ echo d
d
脚本3:
#!/bin/bash
#IFS="\n"
IFS=$"\n"
#IFS=$'\n'
str="a\nb\nc\nd\\n"
echo $str
for i in $str
do
echo $i
done
echo "===================="
str2="anbncndn"
echo $str2
for j in $str2
do
echo $j
done
结果是:
gl@gl:~$ ./text
a b c d
a
b
c
d
====================
a b c d
a
b
c
d
调试为:
gl@gl:~$ bash -x ./text
+ IFS='\n'
+ str='a\nb\nc\nd\n'
+ echo a '' b '' c '' d ''
a b c d
+ for i in $str
+ echo a
a
+ for i in $str
+ echo
+ for i in $str
+ echo b
b
+ for i in $str
+ echo
+ for i in $str
+ echo c
c
+ for i in $str
+ echo
+ for i in $str
+ echo d
d
+ for i in $str
+ echo
+ echo ====================
====================
+ str2=anbncndn
+ echo a b c d
a b c d
+ for j in $str2
+ echo a
a
+ for j in $str2
+ echo b
b
+ for j in $str2
+ echo c
c
+ for j in $str2
+ echo d
d
脚本4:
#!/bin/bash
#IFS="\n"
#IFS=$"\n"
IFS=$'\n'
str="a\nb\nc\nd\n"
echo $str
for i in $str
do
echo $i
done
echo "===================="
str2="anbncndn"
echo $str2
for j in $str2
do
echo $j
done
结果是:
gl@gl:~$ ./text
a\nb\nc\nd\n
a\nb\nc\nd\n
====================
anbncndn
anbncndn
调试为:
gl@gl:~$ bash -x ./text
+ IFS='
'
+ str='a\nb\nc\nd\n'
+ echo 'a\nb\nc\nd\n'
a\nb\nc\nd\n
+ for i in $str
+ echo 'a\nb\nc\nd\n'
a\nb\nc\nd\n
+ echo ====================
====================
+ str2=anbncndn
+ echo anbncndn
anbncndn
+ for j in $str2
+ echo anbncndn
anbncndn
脚本5:
#!/bin/bash
IFS="&"
str="a&&b&&c&&d"
echo $str
for i in $str
do
echo $i
done
结果是:
gl@gl:~$ ./text2
a b c d
a
b
c
d
调试为:
gl@gl:~$ bash -x ./text2
+ IFS='&'
+ str='a&&b&&c&&d'
+ echo a '' b '' c '' d
a b c d
+ for i in $str
+ echo a
a
+ for i in $str
+ echo
+ for i in $str
+ echo b
b
+ for i in $str
+ echo
+ for i in $str
+ echo c
c
+ for i in $str
+ echo
+ for i in $str
+ echo d
d
脚本6:
#!/bin/bash
IFS=$' '
str="a b c d"
echo $str
for i in $str
do
echo $i
done
结果是:
gl@gl:~$ ./text3
a b c d
a
b
c
d
调试为:
gl@gl:~$ bash -x ./text3
+ IFS=' '
+ str='a b c d'
+ echo a b c d
a b c d
+ for i in $str
+ echo a
a
+ for i in $str
+ echo b
b
+ for i in $str
+ echo c
c
+ for i in $str
+ echo d
d
脚本7:
#!/bin/bash
IFS="\n"
#IFS=$"\n"
#IFS=$'\n'
str="ab
d"
echo $str
for i in $str
do
echo $i
done
结果是:
gl@gl:~$ ./text4
ab
d
ab
d
调试为:
gl@gl:~$ bash -x ./text4
+ IFS='\n'
+ str='ab
d'
+ echo 'ab
d'
ab
d
+ for i in $str
+ echo 'ab
d'
ab
d
脚本8:
#!/bin/bash
#IFS="\n"
IFS=$"\n"
#IFS=$'\n'
str="ab
d"
echo $str
for i in $str
do
echo $i
done
结果是:
gl@gl:~$ ./text4
ab
d
ab
d
调试为:
gl@gl:~$ bash -x ./text4
+ IFS='\n'
+ str='ab
d'
+ echo 'ab
d'
ab
d
+ for i in $str
+ echo 'ab
d'
ab
d
脚本9:
#!/bin/bash
#IFS="\n"
#IFS=$"\n"
IFS=$'\n'
str="ab
d"
echo $str
for i in $str
do
echo $i
done
结果是:
gl@gl:~$ ./text4
ab d
ab
d
调试为:
gl@gl:~$ bash -x ./text4
+ IFS='
'
+ str='ab
d'
+ echo ab d
ab d
+ for i in $str
+ echo ab
ab
+ for i in $str
+ echo d
d
1. IFS="\n"和IFS=$"\n"等价,都是以反斜杠\ 和 英文字母n为分隔符,IFS=$'\n'则是以转化后的换行(即为按住Enter键的效果)为分隔符
2.出现多个空格并列为一个空格,多个除空格外的换行符并列出现时,如IFS="\n";str1="a\nb\nc" 或 IFS="&";str2="a&&b&&c" ,此时换行符\和字母n都作为str1的分隔符,&作为str2的换行符,则连续的两个换行符中间会出现一个空字符'',如上面脚本示例中的调试代码中出现的 ''
3. IFS="\n"和IFS=$"\n"都是表示反斜杠\ 和 英文字母n为分隔符,和按住Enter键的效果不是一回事,IFS=$'\n'表示按住Enter键的效果