Cat Command in Bash [With 4 Practical Examples]

Some of you are already familiar with the cat command in Linux which displays the contents of files or concatenate multiple files and display the combined output. However, in this article, I will show you how you can use the cat command in your Bash script.

Key Takeaways

  • Basic of the cat command.
  • Displaying the contents of a file.
  • Concatenating multiple files.
  • Showing the number of lines of a file.
  • Displaying contents of multiple files.

Free Downloads

Practical Examples of Cat in Bash

Here I will provide some examples of the cat command in Bash. Practice them with me.

Example 1: Display the Contents of a File

In the first example, I will show you the most basic use of the cat in Bash. Here, I will take a file path using positional argument and then I will display the contents of the file using the cat command.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano contents.sh

Opening file in Nano for "Cat in Bash"❸ Copy the script mentioned below:

#!/bin/bash

file_path=$1

cat "$file_path"

EXPLANATION

Here #!/bin/bash is the shebang line that specifies the interpreter. Then the file_path=$1 line assigns the value of the first command line argument to the variable “file_path”. Finally, the cat “$file_path” line displays the contents of the file specified by the “file_path” variable.

❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.

❺ Use the following command to make the file executable:

chmod u+x contents.sh

Giving Executing permission❻ Run the script by the following command:

./contents.sh file1.txt

Run contents.sh script for "cat in Bash"

Here the script will take “file1.txt” as an argument and print it on the terminal using the cat command.

Example 2: Concatenate Mulitple Files

Now I will concatenate multiple files and store them in a different file. First, I will take file names as arguments, then concatenate them to another file named “combined txt”.

You can follow the steps of Example 1 to know about creating and saving shell scripts.

Script (concatenate.sh) >

#!/bin/bash

output_file="combined.txt"

cat "$@" > "$output_file"

echo "Files concatenated into: $output_file"

EXPLANATION

Here output_file=”combined.txt” assigns the filename “combined.txt” to the variable “output_file”. Then the cat “$@” > “$output_file” line concatenates the contents of the input files and redirect the output to the file specified by “output_file”. Here, the special variable “$@” represents all command-line arguments passed to the script. Finally, the echo “Files concatenated into: $output_file” line uses the echo command to display a message indicating that the files have been concatenated into the “output_file”.

Now run the script by the following command:

./concatenate.sh file1.txt file2.txt

Running concatenate.sh file

This script has taken two files as input and concatenated them into another file named “combined.txt“.

Example 3: Number of Lines of a File

In this example, I will use cat in Bash to find the number of lines in a file. Here, I will take the file path as the input argument and then count the number using cat and wc commands.

You can follow the steps of Example 1 to know about creating and saving shell scripts.

❶ Open a file by the name of “lines.sh” using the nano text editor and write the following script.

Script (lines.sh) >

#!/bin/bash

file_path=$1

line_count=$(cat -n "$file_path" | wc -l)

echo "Number of lines in $file_path: $line_count"

EXPLANATION

Firstly, file_path=$1 assigns the first command-line argument to the variable “file_path”. Then in line_count=$(cat -n “$file_path” | wc -l), the cat -n “$file_path” command reads the contents of the file and numbers each line and the output is piped to the command “wc -l” which counts the number of lines. Then the number is stored in the variable “line_count”. Finally, in line echo “Number of lines in $file_path: $line_count”, the echo command prints the message where variables $file_path and $line_count are substituted with their respective values.

Execute the script by the command below:

./lines.sh file1.txt

Executing lines.sh file

In the output, you can see, “file1.txt” has two lines.

Example 4: Display Contents of Multiple Files with Headers

Finally, I will show you a script that uses the cat command to display multiple files with a header.

You can follow the steps of Example 1 to know about creating and saving shell scripts.

Script (multiple_files) >

#!/bin/bash

for file in file1.txt file2.txt; do
echo "Contents of $file:"
cat "$file"
echo
done

EXPLANATION

Here this for file in file1.txt file2.txt; do line starts a “for” loop that iterates over a list of files. Then the echo “Contents of $file:” line prints the header where “$file” is substituted with the name of the current file. After that, the cat “$file” command displays the contents of the “$file” file. In addition, the purpose of the last echo command is to print an empty line after printing every file.

❷ Now save the file and make it executable. Follow the first example.

❸ At last, run the script with the following command

./multiple_files.sh

Running multiple_files.sh scriptHere, in the output, the contents of two files are printed with headers. You can also see a space in between.

Conclusion

In this example, I have explained the cat in Bash with multiple practical examples. I hope it was helpful to you.

People Also Ask

What is cat in shell?
Cat is short of concatenate. It is a command that displays the contents of one or more files without opening file in text editor.

How to use cat in shell script?

To use cat command in Linux, you need to type “cat” followed by file name/names. For instance, my filename is file.txt. The command will go like the following: cat file.txt

How to create a cat file in Linux?

To create a file using cat, you need to write the cat command followed by “>” and the filename. For example, you want to create file by the name of myfile, you can use the command below: cat > myfile

Related Articles


<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment