Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Redirect stderr to stdout in Bash - DesignLinux

designlinux 0 Comments

When redirecting the output of a command to a file or piping it to another command, you might notice that the error messages are printed on the screen.

In Bash and other Linux shells, when a program is executed, it uses three standard I/O streams. Each stream is represented by a numeric file descriptor:

  • 0 – stdin, the standard input stream.
  • 1 – stdout, the standard output stream.
  • 2 – stderr, the standard error stream.

A file descriptor is just a number representing an open file.

The input stream provides information to the program, generally by typing in the keyboard.

The program output goes to the standard input stream and the error messages goes to the standard error stream. By default, both input and error streams are printed on the screen.

Redirecting Output #

Redirection is a way to capture the output from a program and send it as input to another program or file.

Streams can be redirected using the n> operator, where n is the file descriptor number.

When n is omitted, it defaults to 1, the standard output stream. For example, the following two commands are the same; both will redirect the command output (stdout) to the file.

command > file
command 1> file

To redirect the standard error (stderr) use the 2> operator:

command 2> file

You can write both stderr and stdout to two separate files:

command 2> error.txt 1> output.txt

To suppress the error messages from being displayed on the screen, redirect stderr to /dev/null:

command 2> /dev/null

Redirecting stderr to stdout #

When saving the program’s output to a file, it is quite common to redirect stderr to stdout so that you can have everything in a single file.

To redirect stderr to stdout and have error messages sent to the same file as standard output, use the following:

command > file 2>&1

> file redirect the stdout to file, and 2>&1 redirect the stderr to the current location of stdout.

The order of redirection is important. For example, the following example redirects only stdout to file. This happens because the stderr is redirected to stdout before the stdout was redirected to file.

command 2>&1 > file 

Another way to redirect stderr to stdout is to use the &> construct. In Bash &> has the same meaning as 2>&1:

command &> file

Conclusion #

Understanding the concept of redirections and file descriptors is very important when working on the command line.

To redirect stderr and stdout, use the 2>&1 or &> constructs.

If you have any questions or feedback, feel free to leave a comment.

bash terminal

Related

Tags: bash, terminal

How to Install and Configure Redis on Ubuntu 20.04

Prev Post

How to Install GCC (build-essential) on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 488
  • 606,238

DesignLinux.com © All rights reserved

Go to mobile version