Bash shell $PIPESTATUS

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

2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence







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

gravatar


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

gravatar


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

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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