6. Word splitting

If the variable is not in double quotes, the shell participles the variable after parameter expansion, command substitution, and arithmetic expansion, such as:

$ echo a   b  c      d
a b c d

The shell takes each character of $IFS as a delimiter, and if $IFS is unset, it has the default value

< TAB >

.

Participles, first of all, ignore the blank of variable first operator < space > < TAB > < newline >, separated from the word.

$IFS = $IFS = $IFS = $IFS

// file test #! /usr/bin/bash if [ -v IFS ]; then echo ==\$IFS=$IFS== else echo ==\$IFS is unset== fi $ ./test ==$IFS= ==

$* and $@ in the for loop

Both $* and $@ represent all positional parameters.

  • Not in double quotes: Both are used in the same way, and both are participles.

    // file test #! /usr/bin/bash echo '==$*==' for name in $* do echo $name done echo '==$@==' for name in $@ do echo $name done $ ./test a  s d fff ==$*== a s d fff ==$@== a s d fff
  • In double quotation marks:

$* splits the word and joins it into a string using the first character of $IFS as a delimiter. “$*” is equivalent to “$1c$2c…” , c represents the first character of $IFS.

$@ also splits words, but does not merge into a string; “$@” is equivalent to “$1” “$2″… , is the same as casting without double quotation marks.

// file test #! /usr/bin/bash echo '==$*==' for name in "$*" do echo $name done echo '==$@==' for name in "$@" do echo $name done $ ./test a s d fff ==$*== a s d fff ==$@== a s d fff

reference

  • Bash Reference Manual:https://www.gnu.org/software/…