As I get used to Python's writing style, I often want to use bash to iterate through the rows of a file and process the rows that are read.
But a weird situation if my text resembles this
The first line.
The second line.
The third line.
For line in ' cat filename ' Doecho $linedone
When I traverse each line of the file in the way above, I find that the data read is this:
The
First
Line.
The
Second
Line.
The
Third
Line.
Obviously, Bash doesn't follow my idea of breaking a newline character as a single line delimiter, but instead of a space as a separator character. This is certainly not the result of my hope, and I am baffled by it.
Until one day I accidentally saw a system variable--ifs about the shell.
With regard to FS, often awk is not unfamiliar, awk has rs,ors,FS, OFS 4 variables that can define delimiters. The IFS in the shell also defines the delimiter.
If you want to achieve the effect I want, then you just need to assign the IFS to a delimiter. But there is a more fastidious, so the question arises
Ifs= ' \ n ' ifs=$ "\ n" ifs=$ ' \ n '
These three assignments appear to be more like "assigning a newline character to ifs," but actually only the last one is the result I want.
Ifs= ' \ n '//character n as the line break of the IFS.
ifs=$ "\ n"//This is actually converted to a newline character by $, but only when interpreted (or executed) is converted to a newline character.
ifs=$ ' \ n '//This is the true line break.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Special variable IFS in the shell