Special variable IFS in the shell

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.