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.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano contents.sh
❸ Copy the script mentioned below:
#!/bin/bash
file_path=$1
cat "$file_path"
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
❻ Run the script by the following command:
./contents.sh file1.txt
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”.
Script (concatenate.sh) >
#!/bin/bash
output_file="combined.txt"
cat "$@" > "$output_file"
echo "Files concatenated into: $output_file"
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
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.
❶ 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"
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
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.
Script (multiple_files) >
#!/bin/bash
for file in file1.txt file2.txt; do
echo "Contents of $file:"
cat "$file"
echo
done
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
Here, 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
Related Articles
- How to Print Output in Bash [With 6 Practical Examples]
- What is Echo Command in Bash [With 3 Practical Examples]
- How to Echo New Line in Bash [6 Practical Cases]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Suppress Output in Bash [3 Cases With Examples]
- How to Change Color of Output Using Bash Script? [Easy Guide]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial