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:
- cmd << delimiter
- Here Document Content
- 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:
- fish@mangos:~$ cat << EOF
- > First Line
- > Second Line
- > Third Line EOF
- > EOF
- First Line
- Second Line
- 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
- cat << EOF > output.sh
- echo "hello"
- echo "world"
- 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:
- echo "hello"
- 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
- cat << EOF > output.sh
- echo "This is output"
- echo $1
- EOF
Run the script sh here. sh HereDocument to get the output. sh content.
- echo "This is output"
- 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
- Cat <"EOF"> output. sh # note the quotation marks
- Echo "hello"
- Echo "world"
- EOF
The output. sh is
- echo "This is output"
- 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.