Bash Find out the exit codes of all piped commands

How do I get exit status of process that’s piped to another (for e.g. ‘netstat -tulpn | grep nginx‘) on a Linux or Unix-like system using a bash shell?

A shell pipe is a way to connect the output of one program to the input of another program without any temporary file. The syntax is:[donotprint]
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 2m

command1 | command2 | commandN


command1 | filter_data_command > output


get_data_command | verify_data_command | process_data_command | format_data_command >

How to use pipes to connect programs

Use the vertical bar (|) between two commands. In this example, send netstat command output to grep command i.e. find out if nginx process exits or not in the system:
# netstat -tulpn | grep nginx
Sample outputs:

Fig.01: Find the exit status of pipe command

How to get exit status of process that’s piped to another

The syntax is as follows to get exit codes of all piped commands:

command1 | command2 
echo "${PIPESTATUS[@]}"


command1 | command2 
echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"

PIPESTATUS is an array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline. Try the following commands:

netstat -tulpn | grep nginx
echo "${PIPESTATUS[@]}"
true | true
echo "The exit status of first command ${PIPESTATUS[0]}, and the second command ${PIPESTATUS[1]}"
true | false
echo "The exit status of first command ${PIPESTATUS[0]}, and the second command ${PIPESTATUS[1]}"
false | false | true 
echo "The exit status of first command ${PIPESTATUS[0]}, second command ${PIPESTATUS[1]}, and third command ${PIPESTATUS[2]}"

Sample outputs:

Fig.02: Use the PIPESTATUS array to get the exit status of each element of the pipeline (click to enlarge)

Putting it all together

Here is a sample script that use ${PIPESTATUS[0]} to find out the exit status of mysqldump command in order to notify user on screen about database backup status:

### Purpose: : Backup database ###
### Author: Vivek Gite <>, under GPL v2.x+ or above. ###
### Change as per your needs ###
set -eo pipefail
# Failsafe? Create dir #
[  ! -d "$DEST" ] && $MKDIR -p "$DEST"
# Filter db names
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
DBS="$($SED -e 's/performance_schema//' -e 's/information_schema//' <<<$DBS)"
# Okay, let us go
for db in $DBS
                tTime=$(date +"${NOWFORMAT}")
                $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
				if [ ${PIPESTATUS[0]} -ne "0" ];
				    echo "The command $MYSQLDUMP failed with error code ${PIPESTATUS[0]}."
				    exit 1
				    echo "Database $db dump successfully."

A note about zsh user

Use the array called pipestatus as follows:

true | true
echo "${pipestatus[1]} ${pipestatus[2]}"


0 0

Conclusion – Finding exit codes of all piped commands

The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled too. Just add the following at the top of your bash shell script:
set -eo pipefail
To pipe output and capture exit status in Bash shell use:

date | grep 2018
echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
false | true
echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"

For more info see:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Naresh kumar Jul 24, 2015 @ 12:31

    Very useful, I use pipe commands a lot.

  • feilong Jul 27, 2015 @ 14:27

    I am learning every day.
    Thanks for sharing.

  • Eduardo Jorge Jul 27, 2015 @ 18:27

    I am a Fedora user and I installed zsh and enabled it, but at the first reboot, I got a menu of configurations options of zsh and I don’t know how to configure code completion and other configuration options. Do you can help me?

  • Johnny Well Jul 27, 2015 @ 19:16

    Very useful, thanks! (especially by the zsh tip).

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.