LKBEN10950: Howto use "Here Documents" in a bash script. This can be used to create scripts in scripts!


Symptom

The "Here Documents" facility are heavily used in advanced shell scripts

Cause

none

Solution

The "Here Documents" is a type of redirection that instructs the shell to read input from the current source until a line containing only a certain word is seen. All of the lines read up to that point are then used as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

Please note that there is no parameter expansion, command substitution, arithmetic expansion, or pathname expansion performed on word.

For more information type "man bash" and search for "Here Documents"

Here are a few examples:

cat <<magicword
the things here are also called the here document body
It can be more than one line long and can be used to give
a command more than just one input. The command in this
case is cat. The next line will stop the input.
magicword

The previous can also be writte like this:

cat <<-magicword
    the things here are also called the here document body
    It can be more than one line long and can be used to give
    a command more than just one input. The command in this
    case is cat. The next line will stop the input.
magicword

This is easier to read. In this case do not forget the minus in front of the magicword! This tells the shell to take the tabs out. Please note that this is an example, the previous scripts can be realised easier by just taking a file as input.

A nice thing to do is an ftp session.

ftp -n $LubbySrv <<SessionEnd
# -n option disables auto-logon

user anonymous "$Password"
binary
cd $Directory
put "$TheFile"
bye
SessionEnd

Please note that quoting or escaping the "string" (here SessionEnd and magicword) at the head of a "here document" disables parameter substitution within its body.

cat <<'magicword'
The parameter substitution is of in this body.
Hello $NAME is written exactly like this.
magicword

This script even writtes a new script at runtime which can be called if it is made executable. This is extremely powerfull.

#!/bin/bash
echo "Hello, creating a new script"
(
cat <<'EOF'
#!/bin/bash
echo "I am the new one!"
exit 0
EOF
) > /tmp/test2.sh
#making the just created script executable
chmod +x /tmp/test2.sh
echo "end"

The following can also be used to write a file or extra script:

echo "Hello"
cat <<'magicword' > /tmp/thefile.txt
Here we create the file /tmp/thefile.txt with here documents.
The file will have the this contents till the word magicword
comes on an extra line without leading space!
magicword
echo "end"

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.