Capturing and saving command output in the Bash shell is an essential skill for effective data management. This article will explore simple techniques and commands to effortlessly save bash output to a file. Mastering this skill will empower you to efficiently handle and store valuable information.
Key Takeaways
- Getting familiar with the process of saving bash output to a file.
- Getting familiar with the process of appending bash output to a file.
Free Downloads
Practical Cases of Saving Bash Output to File
When you run a Bash script, it gives you output. Generally, you see these outputs on the terminal. But you can save these outputs in a file. I have listed some applications for saving bash output to a file.
Case: 01 Save Output to a File by Creating or Overwriting a File
You can easily keep the output of a script into a file by creating or overwriting a file while running the script. Here I will develop a script that will take two parameters then sum the two parameters and keep the result on a file named summation.txt.
➊ At first, open the Ubuntu Terminal.
➋ Then, execute the following command into the terminal.
nano filewrite.sh
➌ This will open a file named filewrite.sh. Now, type the following line into the file and press CTRL+S to save, then CTRL+X to close the file.
#!/bin/bash
a=$1
b=$2
s=$(($a+$b))
echo "Summation of $a and $b is:$s" > summation.txt
➍ Now, run the following command into the terminal to run the filewrite.sh bash script passing 2 and 3 as parameters.
bash filewrite.sh 2 3
➎ Now, type the following command into the terminal to check the existence of the summation.txt file in the current directory.
ls
➏ Then, execute the following command into the terminal to view the contents of the summation.txt file.
cat summation.txt
The above image shows that the bash script has calculated the summation of 2 and 3 and saved the output to the summation.txt file.
Case: 02 Append Output to a File Using Bash Script
You do not always need to create a new file to keep output from a bash script. You can easily append the output of a bash script to an existing file. Here I will develop a bash script named fileapp.sh that will calculate the multiplication of two parameters passed to it and then append the output to the existing file named summation.txt. To achieve so, follow the script given below.
Script (fileapp.sh) >
#!/bin/bash
a=$1
b=$2
s=$(($a*$b))
echo "Product of $a and $b is:$s" >> summation.txt
Now, run the following command into the terminal to run the fileapp.sh bash script passing 2 and 5 as parameters.
bash fileapp.sh 2 5
Then, execute the following command into the terminal to view the contents of the summation.txt file.
cat summation.txt
The above image illustrates that the fileapp.sh bash script has calculated the multiplication of the two parameters 2 and 5 and append the result on the previously existing summation.txt file.
Case: 03 Using tee to Save Bash Output to a File
You can easily keep the output of a bash script into a file as well as print the output on the terminal. Here I will develop a bash script named teeout.sh. This script will calculate the multiplication of the two passed parameters then print the output on the terminal as well as keep it on a file named tee.txt. To do the same, follow the scripts below.
Script (teeout.sh) >
#!/bin/bash
a=$1
b=$2
s=$(($a*$b))
echo "Product of $a and $b is:$s" | tee tee.txt
Now, run the following command into the terminal to run the teeout.sh bash script passing 1 and 2 as parameters.
bash teeout.sh 1 2
Then, type the following command into the terminal to check the existence of the tee.txt file in the current directory.
ls
Finally, execute the following command into the terminal to view the contents of the tee.txt file.
cat tee.txt
The above image shows that the teeout.txt bash script has successfully calculated the multiplication of the two parameters 1 and 2 then prints the output on the terminal as well as keeps the output on a new file named tee.txt.
Conclusion
Mastering the art of saving Bash output to a file empowers efficient data management and analysis. By harnessing the techniques discussed in this article, you can easily store and reference valuable information, enhancing your command line experience and unlocking new possibilities in Bash scripting.
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]
- Cat Command in Bash [With 4 Practical Examples]
- 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