How to Save Bash Output to File? [3 Practical Cases]

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.

Steps to Follow >

➊ 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

EXPLANATION
#!/bin/bash is a shebang, and it specifies the interpreter to be used to execute the script. In this case, it indicates the interpreter to use the bash shell. The a=$1 and b=$2 assign the first command line argument to the variable ‘a’ and the second argument to the variable b. Then, s=$(($a+$b)) calculates the sum of ‘a’ and ‘b’ by performing arithmetic expansion with the ‘$(())’ syntax, and keeps the result to the variable ‘s’. Afterward, echo “Summation of $a and $b is:$s” > summation.txt line uses the ‘echo’ command to print a message containing the values of ‘a’, ‘b’, and ‘s’. Then the script redirected(‘>’) message to a file named  “summation.txt”. The ‘>’ operator creates or truncates the file and then writes the output into it.

➍ 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 output of filewrite.sh bash script is saved on the summation.txt file.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.

Note: You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (fileapp.sh) >

#!/bin/bash

a=$1
b=$2

s=$(($a*$b))

echo "Product of $a and $b is:$s" >> summation.txt

EXPLANATION
The a=$1 and b=$2 assign the first command line argument to the variable ‘a’ and the second argument to the variable b. The s=$(($a*$b)) calculates the multiplication of ‘a’ and ‘b’ by performing arithmetic expansion with the ‘$(())’ syntax. The result is assigned to the variable ‘s’. Afterward, the echo “Product of $a and $b is: $s” >> summation.txt line uses the ‘echo’ command to print a message with the values of ‘a’, ‘b’, and ‘s’. The Bash programmers use the double angel (‘>>’) operator to append the output to a file called “summation.txt”. If the file doesn’t exist, the program will create it.

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 output of the fileapp.sh is appened on the summation.txt file with the previous contents on that file.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.

Note: You can follow the steps of Case 01 to know about creating and saving shell scripts.

Script (teeout.sh) >

#!/bin/bash

a=$1
b=$2

s=$(($a*$b))

echo "Product of $a and $b is:$s" | tee tee.txt

EXPLANATION
The a=$1 and b=$2 assign the first command line argument to the variable ‘a’ and the second argument to the variable b. The s=$(($a*$b)) calculates the multiplication of ‘a’ and ‘b’ by performing arithmetic expansion with the ‘$(())’ syntax. The result is assigned to the variable ‘s’. Afterward, the echo “Product of $a and $b is:$s” | tee tee.txt line uses the ‘echo’ command to print a message that includes the values of ‘a’, ‘b’, and ‘s’. The ‘tee’ command simultaneously displays the message on the terminal and saves it to a file named “tee.txt”. The ‘|’ symbol redirects the output of ‘echo’ to ‘tee’.

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 output of the teeout.sh bash script is saved on the tee.txt file as well as printed on the terminal.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

How to output the bash script to a file?
You should use a double right angle sign (>>) to save the output of a bash script to a file. Bash programmers use it to write the output of bash commands to a file, appending the output with the existing contents of the file. If the file is not present, it creates a new one with the specified name. Technically, both of these operators redirect the standard output to a file.

 

What is the file extension for bash?
Typically, a Bash script file has a .sh extension to make it clear that it is a shell script file. However, we can directly execute it like a binary but we need to put a shebang or hashbang line at the top of the file to declare the interpreter.

What is the standard output of bash?
By default, Bash directs the error stream to stderr and the output stream to stdout. For both stdout and stderr, any characters written to these FDs are displayed in the console where the command was executed. Additionally, Bash assigns the identifier 1 for the stdout FD and 2 for the stderr FD.

What language is Bash script?
The language of the Bash program or script is not an independent one. The bash program or script is mainly written in C language.

How does a bash file work?
A bash script is generally a series of commands written in a file. These are read and executed by the bash program. The program is executed line by line.

Is Bash a programming language?
Bash is a Unix shell and command language written by Brian Fox. It was initially focused to use for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.

Related Articles


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

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment