Word breaker should not be done in here String

Source: Internet
Author: User

We know that in the Shell, after a variable is expanded, if it is not surrounded by double quotation marks, then its expanded value will also be a participle (word splitting, or split word, the term word has been used by the search engine related technology occupied) operation, divided into several words, A few separate arguments are passed to the command.

For example, for example:

$ foo= "1 2 3"

$ how_many_args () {echo $#;}

$ How_many_args $foo

3

$ How_many_args "$foo"

1

But when this variable appears in the here string, shouldn't this word-breaker work? After all, the purpose of the here string is to pass the string as a whole to the standard input of the current command, where the participle is meaningless. The Bash document also says:

3.6.7 here Strings

A variant of Here documents, the format is:

word

The word undergoes brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic E Xpansion, and quote removal. Pathname expansion and word splittingis not performed. The result is supplied as a and a single string to the command in its standard input.

Then the actual implementation of Bash is not like this:

$ foo= "1|2|3"

$ Ifs= ' | '

$ cat <<< "$foo"

1|2|3

$ cat <<< $foo

1 2 3

This command in the other two support here string of the Volkswagen Shell Ksh and zsh output is the original value of Foo 1|2|3, only Bash it to the word, and then with a space join into a string, so it became 1 2 3.

The bug was not discovered until yesterday when bash first implemented here string 13, and the bash author says the next release will fix it. I wonder why this bug has been discovered for so long (perhaps most people are used to wrapping double quotes when using variables), and wondering why Bash's documentation is true and implementation is wrong, which means that the author is implemented according to a specification, and the document is copied, so yes. Then I checked the redirection part of the POSIX specification and found that there was only here document in the specification, without here string. That may be my guess wrong, and I speculate: this grammar is the invention of Bash, but the author did not write the test code, the implementation of the error.

Few online information, I struggled to Google, found a book published in 93 learning the Korn Shell detailed the Hree string this feature of the ins and outs:

This notation first originated in the Unix version of the RC shell, where it's called a "here string." It is later picked up by the Z-Shell, zsh (see Appendix A), from which the Korn shell borrowed it. This notation are simple, easy-to-use, efficient, and visually-distinguishable from regular here-documents.

It was the RC Shell (1989) that invented the variant here string for the first time, and later Zsh (1990) learned it, then Ksh (1993) borrowed it, and the final Bash was copied over 2002 years ago. One thing to say is that I thought zsh this is the shell of the ultimate shell how all is the product of the 21st century, the original 90 years have, learning.

Word breaker is not allowed in here String

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.