Read more

Bash: Find out the exit codes of all piped commands

Claus-Theodor Riegg
September 15, 2015Software engineer at makandra GmbH

Bash stores the exitcodestatus of piped commands in the environment variable PIPESTATUS

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show snapshot

So you can just echo ${PIPESTATUS[@]} to get them all.

13:52:30 ✔ claus:~$ ps ax | grep /usr/bin/ruby
13205 pts/20   S+     0:00 grep --color=auto /usr/bin/ruby

13:52:43 ✔ claus:~$ echo ${PIPESTATUS[@]}
0 0

PIPESTATUS is an array, so you can get the exitcode of an specific command (first pipe):

13:54:20 ✔ claus:~$ echo ${PIPESTATUS[1]}
Posted by Claus-Theodor Riegg to makandra Operations (2015-09-15 13:50)