- 论坛徽章:
- 1
|
关于IFS的疑问
1) 你可先將 IFS 存起來: old_IFS="$IFS"
要取回時再設 IFS="$old_IFS"
(記的雙引號一定要設哦)
2) 若有兩個 command 打在同一行就用到.
3) 這要扯到 command 與 shell 在 IFS 的認定及處理的差異了.
嗯, 我也講不出個所以然... sorry.
我認為 echo 是在 subshell 裡執行的, 而 subshell 在 initial 時會採用原有 IFS .
不過, 我不確定啦.
你可比較如下兩行:
IFS=";"; echo $A
IFS=";" echo $A
差別在於少了一個 ; , 也就是第二行改變了 subshell 的 IFS 而第一行則沒有.
4) 你用 "$*" 才能發現有何不同, 且將 IFS 設在 b 裡面, 再比較如下:
./b 111 & & 222
./b "111 & & 222"
在 man bash 裡, 關於 $* 的說法如下:
- * Expands to the positional parameters, starting from
- one. When the expansion occurs within double
- quotes, it expands to a single word with the value
- of each parameter separated by the first character
- of the IFS special variable. That is, "$*" is
- equivalent to "$1c$2c...", where c is the first
- character of the value of the IFS variable. If IFS
- is unset, the parameters are separated by spaces.
- If IFS is null, the parameters are joined without
- intervening separators.
复制代码 |
|