Understanding Cat Command like cat EOF with Examples

Are you wondering what does is actual difference between cat << EOF , >> , < and > symbol ? Well here is the explanation for most used cat command with examples.

If you are a linux user, you probably know the importance of cat commands. It is one of the most widely used commands in Linux. cat command expands to concatenate files. It is quite useful to read and concat files. cat command writes its output to standard output, ie terminal screen mostly.

With cat command we can read single or multiple file contents or can append both together and view and create new file from the appended result.

I guess you would already have an idea of how cat commands works , so we would not be explaining basics of cat commands. However just for information, listing down some of the basic usages of the command before we go ahead.

Syntax

cat [OPTIONS] [FILE_NAMES]
  • OPTIONS – See man page for cat options. Use cat --help to view all available options.
  • FILE_NAMES – Zero or more file names

[su_label type=”info”]Also check[/su_label] 7 Best download manager Ubuntu / Linux

Cat Command Examples

Here are some of the basic cat commands examples with minimal descriptions :

  • cat /etc/hosts – Displays the host file content present at path /etc/hosts
  • cat abc.txt > pqr.txt – It redirects the output of cat command to file pqr.txt instead of displaying on screen.
  • cat /etc/hosts – Displays the content of file alongwith line numbers.
  • cat abc.txt pqr.txt – Concats the content of two files and displays.
  • cat > file1.txt – Creates new files file1.txt and opens in default editor.

From the image below you can see other options cat provides for usage.

cat command examples

Understanding cat <<

What is it called ? It’s called heredoc as inferred from the WIKI doc found . It is a form of redirection which involves reading input from current shell.

[su_note note_color=”#FFF9C4″]This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen.[/su_note]

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

<<[-]word
          here-document
 [delimiter]

The cat << EOF syntax is very useful when working with multi-line commands or scripts in Bash. Example: While assigning multi-line string to a shell variable, file or a pipe.

What is EOF here ?

In our case, “EOF” is known as a “Here Tag” of heredoc. Basically <<Here (EOF in our case) tells the shell that you are going to enter a multiline string until the “tag” Here(EOF). You can name this tag as you want, it’s often EOF or STOPor you can use some custom name.

Some rules about here tags:

  1. This tag can be any form of string, either uppercase or lowercase. However most people use uppercase just to follow convention.
  2. 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.Ex :
    $ cat >> test <<HERE
    > Hello world HERE <-- Not by itself on a separate line -> not considered end of string
    > This is a test
    >  HERE <-- Leading space, so not considered end of string
    > and a new line
    > HERE <-- Now we have the end of the string

3. The tag should not have any leading or trailing spaces in that line to be considered a tag. Otherwise it is considered as part of the string.

[su_label type=”info”]Also check[/su_label] Best Programming tools for Programmers productivity

Examples :

1. Assign multi-line string to a shell variable

$ sql_command=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='opp'
EOF
)

The $sql_command variable now holds the new-line characters too. You can verify with echo -e "$sql_command".

2. Pass multi-line string to a file in Bash

$ cat << EOF > printed.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF

The printed.sh file now contains:

#!/bin/bash
echo $PWD
echo /home/user

3. Pass multi-line string to a pipe in Bash

$ cat <<EOF | grep 'c|j' | tee b.txt
foo
car
jazz
EOF

The b.txt file contains car and jazz lines. The same output is printed to stdout.

Is cat really necessary here ?

Using cat is not really required as if you test this piece of code, you can pretty much get what point i am trying to make here:

<<test > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
test

will produce the same file as:

cat <<test > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
test

So, I don’t see the point of using the cat command.

[su_label type=”info”]Also check[/su_label] 10 best intellij IDE theme

Understanding cat >>

This command is pretty simple and straightforward. It appends new content in existing file with ‘>>‘ (double greater than) symbol. Contents of file abc.txt will be appended at the end of pqr.txt file.

$ cat abc.txt >> pqr.txt

Feel free to correct me if i was wrong somewhere !

Leave a Reply

Your email address will not be published. Required fields are marked *