“cat” Command in Bash [With 4 Practical Examples]

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

Practical Examples of “cat” Command in Bash

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

1. Display the Contents of a File Using “cat” Command

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. Here’s how:

  1. At first, launch an Ubuntu Terminal.
  2. Write the following command to open a file in Nano:
    nano contents.sh

    Opening file in Nano for "Cat in Bash"

  3. 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.
  5. Use the following command to make the file executable:
    chmod u+x contents.sh

    Giving Executing permission

  6. 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.

2. Concatenate Mulitple Files in Bash With “cat” Command

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”. Now write the bash script to concatenate multiple files:

#!/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 redirects 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.

3. Find Number of Lines of a File Using “cat” Command

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:

#!/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.

4. Display Contents of Multiple Files with Headers With “cat” Command

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

#!/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.

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, if you want to create a file by the name of myfile, you can use the command below: cat > myfile.

What is cat vs ls in Linux?

The ls command without any input argument lists the files and directories of the current directory and can not read the file content whereas the cat command directly reads the file content and displays the contents in the terminal.


Related Articles


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

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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