关于Word Splitting

Word Splitting
      The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
      The  shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters.  If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value  other than  the default...
词分割
      shell扫描不在双引号中发生的parameter expansion, command substitution, and arithmetic expansion操作的结果执行词分割。
      shell把IFS中的每一个字符当做定界符,当见到这些字符时就把展开的结果分割为词。如果IFS没设置或者它的值恰好是默认值<space><tab><newline>,那么前一个展开的结果中在开始或结束位置的<space><tab><newline>序列就被丢弃了,不在开始和结束位置的IFS字符序列用来界定(分割)词。

 应用举例

      比如我要输出IFS的具体字符,在shell脚本中就必须阻止 词分割。

#!/bin/bash
printf "%-4c%-4d\n" ${IFS:0:1} \'${IFS:0:1}
printf "%-4c%-4d\n" ${IFS:1:1} \'${IFS:1:1}
printf "%-4c%-4d\n" ${IFS:2:1} \'${IFS:2:1}

printf "%-4c%-4d\n" "${IFS:0:1}" \'"${IFS:0:1}"
printf "%-4c%-4d\n" "${IFS:1:1}" \'"${IFS:1:1}"
printf "%-4c%-4d\n" "${IFS:2:1}" \'"${IFS:2:1}"

输出:

[root@iZm5eexacs7wlytnyle3n8Z script]# wordsplit.sh 
'   0   
'   0   
'   0   
    32  
       9   

   10  
可见,如果 printf的参数不加双引号的话,输出的值是空,因为IFS中的每一个字符都是 定界符,参数展开的值都被丢弃了,它们都变成了空值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值