# # Bash shell $PIPESTATUS
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Bash shell $PIPESTATUS

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© 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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





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

Printer Friendly Version

->
-> Bash shell $PIPESTATUS

2 comments


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

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.

------------------------


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:

Blog

Linux

Programming

Scripting

Shell

Unix



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode