Linux accumulation-understanding about linux special redirection

Source: Internet
Author: User

Linux accumulation-understanding about linux special redirection

What is Here Document

Here Document is a special redirection method in Linux Shell. Its basic form is as follows:

 
 
  1. cmd << delimiter 
  2. Here Document Content  
  3. delimiter 

It is used to pass the Content (Here Document Content) between two delimiter to cmd as the input parameter.

For example, if you enter cat <EOF in the terminal, the system will prompt you to continue the input, enter multiple lines of information and then enter EOF. The information entered in the middle will be displayed on the screen. As follows:

 
 
  1. fish@mangos:~$ cat << EOF 
  2. > First Line  
  3. > Second Line  
  4. > Third Line EOF  
  5. > EOF  
  6. First Line  
  7. Second Line  
  8. Third Line EOF 

Note:> this symbol is the identifier of the prompt input information generated by the terminal.

Note the following points:

  • EOF is only an identifier and can be replaced with any valid character.
  • Delimiter at the end must be written in the top level. The front cannot contain any character.
  • There cannot be any characters including spaces after delimiter as the end)
  • Spaces before and after delimiter as the start will be omitted
  • Here Document can be used not only on the terminal, but also in the shell file, for example, the following here. sh File
 
 
  1. cat << EOF > output.sh  
  2. echo "hello"  
  3. echo "world"  
  4. EOF  

Run the script file by using sh here. sh, and the new file output. sh will be obtained. The content in the file is as follows:

 
 
  1. echo "hello"  
  2. echo "world"  

Here Document Deformation

Delimiter and variable

In the content of Here Document, you can not only include common characters, but also use variables in it. For example, you can change the preceding here. sh

 
 
  1. cat << EOF > output.sh  
  2. echo "This is output"  
  3. echo $1  
  4. EOF 

Run the script sh here. sh HereDocument to get the output. sh content.

 
 
  1. echo "This is output"  
  2. echo HereDocument 

$1 is expanded as the script parameter HereDocument

But sometimes you don't want to expand this variable. You can add "before and after the start of delimiter. For example, you can change here. sh

 
 
  1. Cat <"EOF"> output. sh # note the quotation marks
  2. Echo "hello"
  3. Echo "world"
  4. EOF

The output. sh is

 
 
  1. echo "This is output"  
  2. echo $1 

<Changed to <-

Here Document also uses '<' To Change '<-'. The only change using <-is that the tab (tab) at the front of each line in the content section of the Here Document will be deleted, this method is used to indent the Content Part Of The Here Document for easy reading of the Code.

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.