What is EOF in a shell script
Ever wondered what is <<EOF in Linux shell scripts? Basically <<EOF tells the shell that you are going to enter a multiline string until the "tag" EOF. You can name this tag as you want, it's often EOF or STOP.
EOF Parameter Example
Let's take below shell script example
sqlplus / as sysdba << EOF
select name, open_mode from v$database;
archive log list;
EOF
The sqlplus command will start sqlplus but now we need to pass sql queries that are not understood by Linux interpreter. The <<EOF parameter will pass sql queries as user inputs directly to sql prompt.
This is done until same EOF parameters is encountered. Some rules to follow:
The tag can be any string, uppercase or lowercase, though most people use uppercase by convention
The tag will not be considered as a Here tag if there are other words in that line. In this case, it will merely be considered part of the string. The tag should be by itself on a separate line, to be considered a tag
The tag should have no leading or trailing spaces in that line to be considered a tag. Otherwise it will be considered as part of the string
Save EOF Output
You can save the EOF output to another file
sqlplus / as sysdba << EOF > output.log
select name, open_mode from v$database;
archive log list;
EOF
This will create a new file output.log in current location which will contain output sql queries.
EOF Examples
Print variable value with EOF
a=10
cat << EOF
$a
EOF
Print multi-line output to a file
cat << EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF
Run rman backup via shell script
rman target / << EOF > backup.log
run
{
crosscheck backup;
backup database plus archivelog;
}
EOF
👨💻👨💻👨💻