APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Bash shell $PIPESTATUS

© June 2004 Tony Lawrence

Referencing: Re: pipes and return code

This Linux newgroup post called my attention to a Bash feature I had never noticed: PIPESTATUS. It's an array that holds the exit status of your last foreground pipeline commands.

For example (this is Bash shell)

ls | wc -k
echo ${PIPESTATUS[1]}

Will give you a 1 because "wc -k" fails.

It's sometimes useful to grab the whole thing:

 ls | wc -k | foo
echo ${PIPESTATUS[*]}

will give you "0 1 127" - unless you have a "foo" command on your system. "ls" is 0, "wc -k" is 1, and the non-existent foo causes the "127".

PIPESTATUS was broken on some earlier Bash shells,

This is obviously useful in scripts that need to be sure of return values.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Bash shell $PIPESTATUS


Inexpensive and informative Apple related e-books:

El Capitan: A Take Control Crash Course

Take Control of Pages

Photos for Mac: A Take Control Crash Course

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of iCloud

More Articles by © Tony Lawrence

Mon Apr 4 10:47:49 2011: 9420   xeros


Thanks for this info, very useful and rather hard to find if don't exactly know what to search for.
Btw. do you know how to achieve simmilar thing in ASH/DASH shell?

Mon Apr 4 11:09:59 2011: 9421   TonyLawrence


If there is anything similar in Dash, I don't see it in the manual.


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

When someone says: "I want a programming language in which I need only say what I wish done", give him a lollipop. (Alan J. Perlis)

Linux posts

Troubleshooting posts

This post tagged:







Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode