Use of the special variable IFS in shell, and the difference between the value of $ '\ n' and' \ n', shellifs
When a for loop is used in shell, the IFS is also used to define the separator. To specify multiple IFS characters, you only need to concatenate them in the value assignment line. As follows:
IFS = $ '\ N ':;"
This assignment uses line breaks, colons, semicolons, and double quotation marks as field separators. There is no limit on how to parse data Using IFS characters.
So what are the following three differences?
IFS = '\ n' // use the character \ and CHARACTER n as the IFS line break. IFS = '\ n' // same as above. IFS = $ '\ n' // use a line break as the field separator. // If IFS = '\ N':; "// This assignment uses the backslash, n, colon, semicolon, and double quotation mark as the field separator.
The test is as follows:
(File log.txt)
Hello \ worldthis is n. jackare you n or y
(Shell1 for testing)
#! /Bin/bashstr = 'cat log.txt 'IFS = $' \ n' for word in $ str; doecho $ worddone/** output: hello \ worldthis is n. jackare you n or y **/
(Shell2 for testing)
#! /Bin/bashstr = 'cat log.txt 'IFS =' \ n' for word in $ str; doecho $ worddone/** output: hello worldthis is. jackare you or y **/