Knowing how to use Cat EOF command is a useful skill to have for displaying the contents of a file. Here’s everything you need to know about this Linux command, with many examples too.
Cat is a widely used feature in Linux that allows displaying the contents of a file. In addition, this command line utility can concatenate the contents of a file. The tool can be combined with other commands such as awk, sed, and less. We will discuss how we can use cat and EOF to perform a range of valuable features, such as concatenating multiline strings.
The Cat Command
Cat command is a Linux utility that can put specified contents into the output stream. If no output stream is specified, it outputs the content to the standard output stream. The following command illustrates the output content of a file to standard output:
$cat file.txt
In the above command, the content of file.txt is output to the standard output stream. To redirect the content to another file, you can use the following command:
$cat file.txt > file2.txt
End of file (EOF)
The EOF character is used in several programming languages, such as C, Java, and Python. It represents the end of the file. In other words, whenever a compiler or interpreter encounters this character, it signifies that the end of the file has been reached. In the bash, the EOF is used similarly to specify the end of the file. When used with the cat operator, it serves a different purpose. It can be used to end the current input from the terminal (called here file). In the following few sections, we will demonstrate several examples of using EOF. The here document has the following format:
The document here is treated as a single word. It continues until the tag or the delimiter is encountered.
Rules for the delimiter / TAG
A tag such as EOF
can be any string, upper or lower case letter. However, generally upper case is used by convention. Primarily, EOF
or STOP
is used as the tag. The tag should be on a separate line to be considered as a tag. Finally, the tag should have no leading or trailing spaces. Otherwise, it will be regarded as part of a string.
Examples of How to Use Cat EOF on Linux
We will now see several examples of using the cat command along with EOF
. In particular, we will see four examples, i.e., assigning multiline input, assigning to a bash variable, assigning an SQL query, and piping multiple commands along with the cat.
Example 1- Writing multiline strings to a file
The cat command is a handy utility, and when combined with the EOF
feature, the multiline string can be written to a file in bash. Type the bash command along with the left shift operator. Next, add the keyword used to terminate input. Then use the redirection operator and the name of the file to write to:
$cat << EOF > file.txt
In the above command, we have told the bash to terminate the input when EOF is encountered. Here, EOF
is the tag used to end the input. Note that a new file will be created if the file doesn’t exist. The <<
operator reads the multiline input string that begins from the next line onwards and treats it as code in a separate file. To verify the contents of the file, type the following command:
$cat file.txt
Tip: The redirection operator <<
and <<-
allows the redirection of the shell input file (called as here document) to the input of a command
Example 2 – Assigning a multiline string to a bash variable
The cat
command can be used to assign a multiline string to a bash variable as well. The following command sets a multiline string to a variable:
comm =$(cat <<EOF
ls /home/khan/Downloads/
rm /home/khan/Downloads/*
EOF
)
Here, the $()
command is used to assign a bash variable.
Example 3 – Assigning the SQL input to a bash variable
Let’s see another example, where we assign the bash multiline input to a psql variable used to execute queries in SQL. The following command can be used to assign the variable to psql:
$ sql=$(cat <<EOF
SELECT id, name FROM employee
WHERE id=101'
EOF
)
The above SQL command selects all the employees with id equals 101. This SQL command is assigned to the SQL variable. This can further be used to execute queries on the terminal.
Example 4 – Piping multiline string
You can also pipe a multiline string to a command. The following example uses cat and EOF features to pipe the multiline string:
cat << EOF | grep 'java' >java.txt
When running the above command, it will prompt you for the input. When the input encounters an EOF, it will terminate. The input is a pipe to grep command, which searches for the keyword java and then redirects them to the file java.txt.
Tip: The redirection operator >
redirects the input to the file overwriting existing contents. The >>
operator, however, appends the content to the file.
We discussed how the cat command could be used in combination with EOF to perform a range of practical operations. For instance, we can write multiline strings to a file and pipe multiline strings in bash, all of which you can find in the multiple examples we have highlighted above.
If this guide helped you, please share it.