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.
cat [OPTIONS] [FILE_NAMES]
OPTIONS– See man page for cat options. Use
cat --helpto view all available options.
FILE_NAMES– Zero or more file names
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.
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.
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]
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
STOPor you can use some custom name.
Some rules about here tags:
- This tag can be any form of string, either uppercase or lowercase. However most people use uppercase just to follow 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.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.Also check Best Programming tools for Programmers productivity
1. Assign multi-line string to a shell variable
$ sql_command=$(cat <<EOF SELECT foo, bar FROM db WHERE foo='opp' EOF )
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
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
b.txt file contains
jazz lines. The same output is printed to
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.Also check 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 !