Capturing and saving command output in the Bash shell enables the user to preserve valuable information on file. It is an essential skill for effective data management. This article will explore simple techniques and commands to save bash output to a file. Such as by using the redirection process, the tee command, the printf command, and the heredoc.
5 Practical Cases of Saving Bash Output to File
When you run a script, it is supposed to give you output. These outputs are generally displayed on the terminal. But you can also save these outputs in a file for later use or further processing. I have listed some cases for saving bash script output to a file.
1. Save Output to a File by Creating or Overwriting a File
Use the output redirection operator ‘>’ to save the output to a file. This operator creates a new file specified in the command if none with the same name exists or overwrites if there is one existing. Here is a bash script to save the script output to a file using the redirection operator:
#!/bin/bash
a=$1
b=$2
s=$(($a+$b))
echo "Summation of $a and $b is:$s" > summation.txt
The script command, s=$(($a+$b))
calculates the sum of ‘a’ and ‘b’ by performing arithmetic expansion with the ‘$(())’ syntax, and the result is assigned to the variable ‘s’. Afterwards, 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 message is redirected to a file named “summation.txt” by (‘>’) operator.
The above image shows that the summation of 2 and 3 is calculated and written on the summation.txt file. Later, I displayed the file content using the cat command just to show you that the result was saved successfully inside the file.
2. Append Output to a File using Bash Script
The appending redirection operator ‘>>’ can also be used to save the output to a file. The main difference between this operator and the output operator ‘>’ is that it appends the output to the bottom of a pre-existing file instead of overwriting it. Here is a bash script to calculate the multiplication of two parameters passed to it and then append the output to an existing file named summation.txt:
#!/bin/bash
a=$1
b=$2
s=$(($a*$b))
echo "Product of $a and $b is:$s" >> summation.txt
The code part, 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’. Afterwards, 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 ‘>>’ operator is used to append the output to a file called “summation.txt”. If the file does not exist, it will be created.
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
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.
3. Using “tee” Command to Save Bash Output to a File
In Bash, the tee command is used to keep the output of a bash script into a file as well as print the output on the terminal. Here is a bash script to 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:
#!/bin/bash
a=$1
b=$2
s=$(($a*$b))
echo "Product of $a and $b is:$s" | tee tee.txt
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 is used to simultaneously display the message on the terminal and save it to a file named “tee.txt”. The piping symbol ‘|’ is used to pipe the output of the echo command to the tee command.
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
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.
4. Using “printf” Command to Write to File
The printf command allows users to display a specified string number on the terminal. With the redirection command, this string can be written to a file, too. For this, execute the command below:
printf “Hello Everyone. \nMy name is %s. \n” $(whoami) > intro.txt
cat intro.txt
The printf
command prints formatted output. Here Hello Everyone. \nMy name is %s. \n
will print two lines separately where %s is the format specifier that will be replaced by the output of the whoami command. After that, this command output will be redirected to the intro.txt file instead of being displayed to the terminal screen.
5. Using “heredoc” to Write to File
A heredoc followed by a delimiter token can include multiple lines of text in a command. To write multiple lines to a file, follow the below Bash commands:
cat << EOF > countries2.txt
cat countries2.txt
The command, cat << EOF> countries2.txt
incorporates here doc to write multiple lines to file. The ‘EOF’ is the arbitrary delimiter. Until it encounters the same delimiter (‘EOF’), everything should be treated as input and redirected to the countries2.txt file.
Conclusion
Learning how to save Bash output to a file improves data management and analysis. By mastering the techniques in this writing, you can effortlessly store and access valuable information, enhancing your command line skills and opening up new opportunities for Bash scripting.
People Also Ask
How to output the bash script to a file?
Use a right angle sign (>) or a double right angle sign (>>) to save the output of a bash script to a file. The ‘>’ is used to write (if the file is not present, it creates a new one with the specified name) or overwrite the output of bash commands to a file, and the ‘>>’ appends the output with the existing contents of the 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 File Descriptors 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 is Bash scripting language. The bash interpreters that executes the bash scripts are 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?
Yes, Bash is a Unix shell and command language written by Brian Fox 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
- 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
FUNDAMENTALS A Complete Guide for Beginners