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 redirect standard output and standard error to the same 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 redirect standard output and standard error to same file
  • How to redirect standard output and standard error to file and terminal
  • How to redirect standard output and standard error to /dev/null
Various Bash examples for redirecting standard output and standard error to the same file
Various Bash examples for redirecting standard output and standard error to the same file
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Redirect standard output and standard error


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Let's first cover how to redirect standard output to a file. If you already have these Bash basics down, feel free to skip ahead to the example commands.

You normally use > for redirection, and to append to an existing file, you would use >>.

$ echo "linuxconfig.org" > new-file.txt
AND
$ echo "linuxconfig.org" >> existing-file.txt

With these commands, standard output would be redirected to the file, and standard error is shown on your screen. In case you're new to Linux, "standard output" is just the usual, expected output from running a command or script, and "standard error" is any error messages that occur.

To redirect standard error instead, we can use 2>. This will make standard output appear on our screen, and send all error messages to a file.

$ echo "linuxconfig.org" 2> new-file.txt

With the basics out of the way, we'll now cover various command examples for redirecting standard output and standard error to the same file.

  1. To redirect standard output and standard error to the same file, use the following command syntax. Specifically, append 2>&1 to the end of your usual command.
    $ echo "linuxconfig.org" > new-file.txt 2>&1
    


  2. A slightly easier way to achieve this functionality is with the &> operator. Note that this works fine in Bash and zsh, but not other shells, so use the previous example for maximum compatibility with other systems.
    $ echo "linuxconfig.org" &> new-file.txt
    
  3. To append standard output and standard error to a file that already exists, use the same syntax above, but with the >> redirection operator.
    $ echo "linuxconfig.org" >> existing-file.txt 2>&1
    
  4. To suppress standard output and standard error from your terminal, and avoid generating a file as well, you could redirect both of them to /dev/null.
    $ echo "linuxconfig.org" > /dev/null 2>&1
    
  5. To redirect standard output to one file, and redirect standard error to a different file, use the following syntax.
    $ echo "linuxconfig.org" 2> std-err.txt 1> std-out.txt
    


  6. If you want to redirect standard output and standard error to the same file, while also seeing them both in your terminal, we can get a little help from the tee command.
    $ echo "linuxconfig.org" 2>&1 | tee new-file.txt
    
  7. If you want to see standard output and standard error on your screen, while appending them to the same file, we can use the -a (append) option with tee.
    $ echo "linuxconfig.org" 2>&1 | tee -a existing-file.txt
    

Closing Thoughts

In this guide, we saw how to redirect both standard output and standard error to the same file in Bash. This included several different Bash operators, depending on whether we want to create a new file or append to an existing file. We also learned how to use the tee command in order to see standard output and standard error on our terminal while also writing them to a file. You're now an expert at redirecting standard output and standard error to a file in Bash, and are that much closer to mastering the shell entirely.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.