The Bash shell is the most popular shell on Linux systems, and to use the shell efficiently, you need a little knowledge about Bash shell redirections. This is also an essential step in learning Bash scripting.
In this guide, we’ll show how to append text or command output to a file on the Bash shell command line. This will include several examples so you can pick the right method in any scenario.
In this tutorial you will learn:
- How to append text or command output to a file
- How to append and view command output at the same time
- How to append multiple lines of text to a file
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to append to file in Bash
To make a new file in Bash, you normally use
> for redirection, but to append to an existing file, you would use
>>. Take a look at the examples below to see how it works.
- To append some text to the end of a file, you can use
echoand redirect the output to be appended to a file. If the file we specify doesn’t already exist, it will be created for us.
$ echo "Linux is great" >> file.txt
- You can also redirect command output to a file. In this example, we append the current date to a file.
$ date >> file.txt
Here’s how our file looks so far.
$ cat file.txt Linux is great Fri 09 Apr 2021 12:11:12 PM EDT
- You may notice that when we redirected the
datecommand output, nothing appears in our terminal. That’s because the output was directed to the file, and not our terminal. We would, however, see errors if any had occurred. To see the command output while simultaneously redirecting to a file, you can pipe to
tee. Notice we must also use the
-aoption with tee.
$ uptime | tee -a file.txt 12:16:46 up 7 min, 1 user, load average: 0.06, 0.64, 0.48
And now our file looks like this.
$ cat file.txt Linux is great Fri 09 Apr 2021 12:11:12 PM EDT 12:16:46 up 7 min, 1 user, load average: 0.06, 0.64, 0.48
- If you want to append multiple lines to a file, you can use
echo -eand separate each line with a
\n(newline character). Here’s what it’d look like.
$ echo -e "Bash is my favorite shell. \nZ shell is alright too." >> file.txt
And the results…
$ cat file.txt ... Bash is my favorite shell. Z shell is alright too.
- Another option for appending multiple lines to a file is using the Heredoc functionality of Bash. This allows you to pass multiple lines into a file. You need to specify a “delimiter” at the beginning of your command, which is normally just
EOF(end of file) but can technically be anything. Here’s what it would look like if we were appending three lines into a file.
cat << EOF >> file.txt My user account is: $(whoami) My home directory is: $HOME Pretty cool, huh? EOF
And the results…
$ cat file.txt ... My user account is: linuxconfig My home directory is: /home/linuxconfig Pretty cool, huh?
That should be all the different syntaxes that you need to know for appending text or command output to a file in Bash. If we look at our file, you can see that all of our examples have been appended to the file we were working with. Thus, all these methods provide the same result, but some are more convenient in certain scenarios.
In this guide, we saw how to append to a file in the Bash shell. This included the standard
>> redirection, as well as using the
tee command and Heredoc function. You’re now an expert at appending to a file in Bash, and are that much closer to mastering the shell entirely.