FUNDAMENTALS A Complete Guide for Beginners
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:
- 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"
EXPLANATIONHere #!/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, thecat "$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.
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"
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
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"
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.
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
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
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
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
- 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