Bash中的单词拆分,IFS设置为非空白字符

我正在经历一个Bash tutorial,特别是单词分裂的主题.

这个名为“args”的脚本有助于演示单词拆分示例:

#!/usr/bin/env bash
printf "%d args:" $#
printf " <%s>" "$@"
echo

一个例子:

$./args hello world,here is "a string of text!"
5 args: <hello> <world,> <here> <is> <a string of text!>

到现在为止还挺好.我理解这是如何工作的.

但是,当我用非空格字符替换IFS时,例如:如果我直接将字符串作为参数传递,则脚本不会执行分词.

$./args one:two:three
1 args: <one:two:three>

但是,如果I(1)将字符串分配给变量,然后(2)通过参数扩展将字符串传递给脚本,则脚本会对同一字符串执行分词.

$IFS=:
$variable="one:two:three"
$./args $variable
3 args: <one> <two> <three>

为什么?具体来说,当IFS未设置且分隔符是空白字符时,为什么传递字符串作为参数会进行单词拆分,但是当IFS设置为非空格字符时却不会?

当我使用read而不是这个脚本时,相同的字符串也会按预期进行单词拆分.

$IFS=:
$read a b c
one:two:three
$echo $a $b $c
one two three
您可以阅读有关单词拆分 here的更多信息.

The shell scans the results of parameter expansion,command substitution,
and arithmetic expansion that did not occur within double quotes for word splitting.

当您传递裸字符串one:two:three作为参数且IFS设置为:时,Bash不进行分词,因为裸字符串不是参数扩展,命令替换或算术扩展上下文之一.

但是,当为变量分配相同的字符串并且将变量传递给未加引号的脚本时,会发生单词拆分,因为它是参数扩展的情况.

同样适用于这些(命令替换):

$./args $(echo one:two:three)
3 args: <one> <two> <three>

$./args "$(echo one:two:three)"
1 args: <one:two:three>

作为documented,读取命令在每行读取时都会进行分词,除非IFS已设置为空字符串.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...