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
EOF is just an identifier and can be replaced with any legal character.
As the end of the delimiter must be shelf write, the front can not have any characters
You cannot have any characters (including spaces) after the end of the delimiter.
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
$1
is 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)