This article is reproduced from http://blog.chinaunix.net/uid-22566367-id-381955.html
Self-documenting content. The shell script that encountered the relevant content in the project. Record in this place
string1="111 222 333 4444"echo $STRING 1echo"$ STRING1"
The output of both pieces of code is
111 222 333 4444
The second piece of code
string2="111 222 333 444"echo $STRING 2echo" $STRING 2"
Output Result:
111 222 333 444 111 222 333 444
The reason for this is the IFS variable: IFS is a shell-defined variable, the default value is <space><tab><newline>3, the role of IFS is to put a string of such a character (and its same character, For example, the above <space>,<tab> <newline> escape is a delimiter, for example the above string is actually
111<space>222<space>333<space><space><space>444
Because IFS also has a <spce> in it, so in his eyes <space> is the delimiter, it is adjacent to the continuous division to meet and together. And the strings wrapped in double quotes are actually shielding the IFS, and when I make the following changes, the output is exactly the same.
ifs='&'STRING2="111 222 333 444" Echo $STRING 2 Echo " $STRING 2 "
Another variable is $*, which refers to a string collection of script entry parameters. Examples are as follows:
A command was entered at the terminal, as follows:
111 222 333
The output of its echo $* is
111 222 333
Next look at the following code
ifs='&'STRING2="111&222&&333&&& 444"echo $STRING 2echo"$STRING 2"
Its output is
111 222 333 444 111&222&&333&&&444
Because ifs= ' & ', the & in the string is completely delimited, so the first output is 111 222 333 444, followed by a set of code. (Note also that not all separators are merged.) Where spaces are merged, for example)
Now run at Terminal
111 222 333 444 IFS='&'echo $*echo'$*'
Its output is
111 222 333 444 111&222&333&444
Because $* equals 111ifs222ifs333ifs444, also equals 111&222&333&444, because Echo $* when,& represents a delimiter, it is converted to a space, and the second line because of the "" Effect makes & No conversions Made
IFS and $* variables in the shell