Word splitting in Bash with IFS set to a non-whitespace character

后端 未结 1 1594
走了就别回头了
走了就别回头了 2020-12-10 16:44

I\'m going through a Bash tutorial, and specifically the subject of word splitting.

This script, called \"args\", helps demonstrate word splitting examples:

相关标签:
1条回答
  • 2020-12-10 17:03

    You can read more about word splitting here.

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

    When you pass the bare string one:two:three as an argument with IFS set to :, Bash doesn't do word splitting because the bare string is not one of parameter expansion, command substitution, or arithmetic expansion contexts.

    However, when the same string is assigned to a variable and the variable is passed to the script unquoted, word splitting does occur as it is a case of parameter expansion.

    The same thing applies to these as well (command substitution):

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

    As documented, read command does do word splitting on every line read, unless IFS has been set to an empty string.


    0 讨论(0)
提交回复
热议问题