IFS in Shell

Source: Internet
Author: User

1. Introduction to IFS

The shell script has an internal domain separator called ifs (internal field seprator. The complete definition is the shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when implements Ming variable substitution.

When shell processes "command replacement" and "parameter replacement", shell splits the read variables based on the IFS value, which is space, tab, and newline by default, and then processes the special characters, finally, re-combine and assign the value to the variable.

The only way to use IFS is to understand the shell execution sequence. In Chapter 7th of the shell script Learning Guide, there is a graph on page 179, which is clear and clear. On the command line, how to deal with single quotes, double quotation marks, and references without quotation marks is clear, and the timing of IFS string separation.
In general, double quotation marks and single quotation marks can be used to block the use of IFS to separate command parameters.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/0E/wKiom1P19HziCeSLAAJeUKSJVl0900.jpg "Title =" 1.png" alt = "wkiom1p19hziceslaajeuksjvl0900.jpg"/>

As shown in, shell commands are parsed and executed in sequence. In step 1 and step 2, a split operation is performed. In step 1, we enter the command on the command line, and shell will first split them as tokens Using IFS as the separator, and then go to step 1, after parameter extension and command replacement, the results are also separated by ifs and split into word.

Note the following:

  1. IFS is the space, tab, and newline characters by default. Multiple consecutive blank spaces are treated as one processing task.

  2. "$ *" Uses the first character in IFS as the separator to connect Parameters

  3. The FS (domain separator) in awk has similar usage and functions as IFS.


Ii. Simple ifs instance

1. view the IFS Value

[[email protected] ~]# set | grep IFSIFS=$‘ \t\n‘[[email protected] ~]# echo -n "$IFS" | od -ab0000000  sp  ht  nl        040 011 0120000003

2. Similarities and Differences between "[email protected]" and "$ *"

* Expands to the positional parameters, starting from one. when the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. that is, "$ *" is equivalent to "$ 1C $ 2C... ", where C is the first character of the value of the IFS Variable. if ifs is unset, the parameters are separated by spaces. if ifs is null, the parameters are joined without intervening separators.

@ Expands to the positional parameters, starting from one. when the expansion occurs within double quotes, each parameter expands to a separate word. that is, "[email protected]" is equivalent to "$1" "$2 "... if the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. when there are no positional parameters, "[email protected]" And [email protected] expand to nothing (I. E ., they are removed ).

"[Email protected]" "$ *" is different only when quoting is used in double quotes. [Email protected] $ * the two results are the same.


This article is from the share your knowledge blog, please be sure to keep this source http://skypegnu1.blog.51cto.com/8991766/1543319

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.