IFS and $ * variables in shell

Source: Internet
Author: User
Tags what echo

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

I have to say that the global variables in shell are all disgusting guys, especially this guy ---- IFS, Which is disgusting and almost fooled by it. So please record it as soon as possible so that it will not be fooled again !!!

STRING1 = "111 222 333 444"

Echo $ STRING1

Echo "$ STRING1"

There is nothing to say about this set of code. The output is 111 222 333 444. Its role is to compare with the following section !!

STRING2 = "111 222 333 444"

Echo $ STRING2

Echo "$ STRING2"

The output result is

111 222 333 444

111 222 333 444

Why ?? The result of the first output is obviously incorrect. Is the STRING1 value output obviously impossible? This is the ghost trick of IFS...

First, let's introduce this guy. IFS is a variable defined in shell. The default value is <space> <tab> <newline> 3. Sorry, I can't see the three values. I can only write them in English. Then what is the function of IFS? its function is to take the XX character in the string (same as it, for example, the preceding <space> <tab> <newline>) convert to Separator

For example, "111 222 333 444", this string is actually 111 <space> 222 <space> 333 <space> 444, because IFS also has a <space>, <space> is a separator in its eyes, therefore, "111 222 333 444" seems like this: 111 <separator> 222 <separator> 333 <separator> 444, because separators (not all separators are merged, and spaces are exceptions) are merged, the 111 <separator> 222 <separator> 333 <separator> 444, the damn <separator> is displayed as a space (or
Space is used to indicate the separator), so it is displayed as "111 222 333 444? Depressed

So what echo $ STRING2 shows is "111 222 333 444", echo "$ STRING2" remains unchanged because "" It shields the IFS function and has not been persecuted by it !!

If I change it like this, the result will be the same. At this time, shell won't regard space as a separator, but the & symbol will be unlucky...

IFS = '&'

STRING2 = "111 222 333 444"

Echo $ STRING2

Echo "$ STRING2"

In addition, there is a global variable of $ *, which is a zombie of IFS and is used as a bad thing. Today, the old version is revealed !!

$ * Refers to the string set of the script entry parameters, which cannot be clearly stated. For example:

Enter a script on the terminal with three parameters.

./Script 111 222 333

The echo $ * output is 111, 222, 333, which is no problem, but the following things will be fooled.

First, this code

IFS = '&'

STRING2 = "111 & 222 & 333 & 444"

Echo $ STRING2

Echo "$ STRING2"

The output is

111 222 333 444

111 & 222 & 333 & 444

After the above explanation, this cannot be fooled ----- because IFS = '&', the & in the string is completely a separator, so the first output is 111 222 333 444, there is also a set of code

Run./script 111 222 333 444

IFS = '&'

Echo $ *

Echo "$ *"

The output is

111 222 333 444

111 & 222 & 333 & 444

Let me explain why

Because $ * is equivalent to 111IFS222IFS333IFS444, or 111 & 222 & 333 & 444. Because when echo $ * is used, & represents a separator, it must be converted to a space, the second line does not undergo conversion due to the role "".

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.