小试 IFS 环境变量

今晚看《Linux Command Line and Shell Scrpiting Bible 2nd editon》说到了 internal field separator(内部字段分隔符),然后我研究了一下,因为我有点疑惑,百度了一下,最终解决了

 1 #!/bin/bash
 2 file="/home/tcstory/desktop/states"
 3 ifs=$IFS
 4 #IFS.OLD=$IFS  在这里 我不能用xxx.xxx来接收值,但是书上就是这么写的,可能是终端的类型不同,我这个不是ubuntu默认的模拟终端器
 5 IFS=$'
'
 6 if [ -f $file ];then
 7     for state in `cat $file`
 8     do
 9         echo "Visit beautiful $state"
10     done
11 else
12     echo The file does not exist.!
13 fi
14 IFS=:!$'40'
15 var1="Hello:world!take your time"
16 for word in $var1
17 do
18     echo $word
19 done

  说一下我的疑惑,在给IFS赋值的时候,如果要赋予的是空格 制服表 和 换行符 ,那么必须用IFS=$' '    IFS=$' '  或则 IFS=$'40' 的形式,如果是普通的字符就不用 例如 IFS=: ;(冒号 感叹号) 我研究了一下,自己在终端输入了 如下命令

echo $' '

echo $' '

echo $'40'

他们确实打印出了相应的空白行,所以,这就验证了,想得到换行符 制服表 和空格 就必须用上面的那种形式才能正确的赋值

原文地址:https://www.cnblogs.com/tcstory/p/3366226.html