Use of the special variable IFS in shell and its value is $ & #039; \ n & #039; and & #039 ;\\ n & #039, shellifs

Source: Internet
Author: User

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 **/

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.