Here document usage for Linux shell (cat << EOF)

Source: Internet
Author: User

What is here Document

Here Document is a special redirection method in the Linux Shell, and its basic form is as follows

CMD << delimiter here Document contentdelimiter

Its role is to pass the contents of the two delimiter (hereDocument content section) to cmd as input parameters.

For example, in the terminal input cat << EOF , the system will be prompted to continue to enter, enter multiple lines of information and then input EOF, the intermediate input information will be displayed on the screen. As follows:

[Email protected]:~$ cat << eof> first line> Second line> third line eof> Eoffirst Linesecond Linethird Line EOF

Note: > This symbol is the identifier for the prompt input information generated by the terminal

Here are some points to note

    1. EOF is just an identifier and can be replaced with any legal character.

    2. As the end of the delimiter must be shelf write, the front can not have any characters

    3. You cannot have any characters (including spaces) after the end of the delimiter.

    4. Spaces before and after the beginning of the delimiter are omitted.

Here document can be used not only on terminals, but also in shell files, such as the following here.sh file

Cat << eof > Output.shecho "Hello" echo "World" EOF

Using sh here.sh run this script file, will get output.sh this new file, inside the content is as follows

echo "Hello" echo "World"
Deformation delimiter and variables for here document

In the content of here Document, you can include not only ordinary characters, but also variables in it, for example, to change the here.sh above to

Cat << eof > Output.shecho "Hello" echo "World" EOF

Use sh here.sh HereDocument run script to get output.sh content

echo "This is output" echo heredocument

$1is expanded here to be the parameter of the scriptHereDocument

But sometimes do not want to expand this variable, can be done by the beginning of the delimiter before and after adding " , for example, the above here.sh to

Cat << EOF > output.sh #注意引号echo "Hello" echo "World" eof

The content of the obtained output.sh is

echo "This is output" echo $? Into?-

Another use of here Document is to turn '? ' into '?-'. The only change that is used is that the <<- tab (tab) in front of each line of the content portion of the here document will be deleted, and this usage is intended to indent the content part when writing here document, so that you can easily read the code.

Reference links

Wiki:here Document
Learn Linux, 101:streams, pipes, and redirects

This article is from the creator think blog, so be sure to keep this source http://strongit.blog.51cto.com/10020534/1736580

Here document usage for Linux shell (cat << EOF)

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.