IFS and $* variables in the shell

Source: Internet
Author: User

This article is reproduced from http://blog.chinaunix.net/uid-22566367-id-381955.html

Self-documenting content. The shell script that encountered the relevant content in the project. Record in this place

string1="111 222 333 4444"echo  $STRING 1echo"$ STRING1"

The output of both pieces of code is

111 222 333 4444

The second piece of code

string2="111 222  333   444"echo  $STRING 2echo"  $STRING 2"

Output Result:

111 222 333 444 111 222  333   444

The reason for this is the IFS variable: IFS is a shell-defined variable, the default value is <space><tab><newline>3, the role of IFS is to put a string of such a character (and its same character, For example, the above <space>,<tab> <newline> escape is a delimiter, for example the above string is actually

111<space>222<space>333<space><space><space>444 

Because IFS also has a <spce> in it, so in his eyes <space> is the delimiter, it is adjacent to the continuous division to meet and together. And the strings wrapped in double quotes are actually shielding the IFS, and when I make the following changes, the output is exactly the same.

ifs='&'STRING2="111 222  333   444" Echo $STRING 2 Echo " $STRING 2 "

Another variable is $*, which refers to a string collection of script entry parameters. Examples are as follows:

A command was entered at the terminal, as follows:

111 222 333

The output of its echo $* is

111 222 333

Next look at the following code

ifs='&'STRING2="111&222&&333&&& 444"echo  $STRING 2echo"$STRING 2"

Its output is

111  222  333   444 111&222&&333&&&444

Because ifs= ' & ', the & in the string is completely delimited, so the first output is 111 222 333 444, followed by a set of code. (Note also that not all separators are merged.) Where spaces are merged, for example)

Now run at Terminal

111 222  333   444 IFS='&'echo $*echo'$*' 

Its output is

111 222 333 444 111&222&333&444

Because $* equals 111ifs222ifs333ifs444, also equals 111&222&333&444, because Echo $* when,& represents a delimiter, it is converted to a space, and the second line because of the "" Effect makes & No conversions Made

IFS and $* variables 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.