How to Use the cat Command in Bash?

The command “cat” is an abbreviation for “concatenate” and is utilized to display the text within one or multiple files on the terminal. It is simple yet powerful and can be used in various ways to view, manipulate, and combine text files. 

This article delves into the fundamental application of the cat command and presents various examples demonstrating how it can enhance the ease and efficiency of managing text files in Bash.

Basic Understanding of the cat Command in Bash

In this section, we will be explaining how you can create and execute along with the cat command. 

Create a Bash Script

The first thing you need to do is to create a bash file, as shown below:

$ vim bashfile.sh

This will open a vim text editor where you must write your bash script. The first thing that you need to do is to write the following lines inside it:

#!/bin/bash

cat File_Name.txt

The #!/bin/bash is called a shebang line, and it is used at the beginning of a bash script to specify that the bash interpreter should be used to execute the script. Subsequently, we utilized the cat command to examine the contents of the “File_Name.txt” file. 

Press the “ESC” button and then type “:wq” to save and exit the vim editor.

Execute a Bash Script

By following the above discussion, you can see that creating and executing a bash script is easy, and in the next section, we’ll delve deeper into the usage of the cat command in bash scripting.

Using the cat to Display the Content of a Single File

You can show the content of any file by writing the name of that file after writing the cat command in the bash script as shown below:

#!/bin/bash

# We will use the cat command to display the contents of the file

cat file1.txt

Now when you execute the bash script you will get the following result:

$ bash bashfile.sh

The line Hello, welcome to the cat command tutorialis saved inside the mentioned file, which is why this line can be seen in the output of the terminal.

Using the cat Command to Create New Files

You can also create a new file inside a bash script using the cat command. Then you can save the content inside it as well using the “Redirection operator (>)” as shown below:

# The Text followed by the echo command will be saved in file1.txt using ">" operator 

echo "Hello, welcome to the cat command tutorial" > file1.txt

# Now we can use the cat command to display the contents of the file1.txt

cat file1.txt         

This line of code is using the echo command to output the string “Hello, welcome to the cat command tutorial” and redirecting it to a file named “file1.txt” using the > operator.

Using the cat Command to Display the Content of Multiple Files 

You can display the content of multiple files as well. All you need to do is to mention the file name followed by the cat command as shown below:

#!/bin/bash

# We will use the cat command to display the content of two different files

cat file1.txt file2.txt

Now, run the bash script to see its output as shown below:

$ bash bashfile.sh

You can see in the above image that both files’ output can be seen in a separate line.

Using the cat Command to View Line Numbers

Sometimes you want to see the number of lines a text file contains, so in such a scenario, you can use the “pipe (|) operator” along with the “nl (Line Numbers)” command as below: 

#!/bin/bash

# Assign numbers to each separate line

cat file3.txt | nl

In this example, cat command will print the output of the “file3.txt” file. That output will become an input of the nl command using a | operator. Then this command will print the line numbers of each individual line or paragraph, as shown in the image below.

$ bash bashfile.sh

As you can see in the above image each line is starting with a number telling you how many paragraphs are there in a text fi;e

Using the cat Command to Convert Small Alphabets to Capital

In this example we will be converting the text written in small letters to the capital letters as shown in the written script below:

#!/bin/bash

# Convert all small alphabets to capital using the tr command

cat file3.txt | tr a-z A-Z

In the above example, the tr stands for translate where we are telling it to convert small letters starting from a-z to capital letter A-Z.

$ bash bashfile.sh

The result of the bash script can be seen as all the small letters are now converted into capital letters.

Conclusion

The “cat” command is a command-line utility in Linux and Unix-based operating systems that is used to concatenate and display the contents of one or more files. In this article, we have explained how you can incorporate the cat command within a bash script along with some examples for your better understanding.