1,105,177 Community Members

understanding $PIPESTATUS

Member Avatar
lewashby
Posting Pro
559 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
( make 2>&1 | tee make.log && exit $PIPESTATUS ) &&

Could someone give me a detailed explanation of the above line? I know that tee is creating a text file while also allawing the output to come to the screen but need a better understanding of $PIPESTATUS. Is "exit" exiting everything to the left of it or $PIPESTATUS to the right? Why are there two sets of "&&"? And I'M sure the variable $? comes it to play somewhere here. Thanks.

Member Avatar
JasonHippy
Practically a Posting Shark
889 posts since Jan 2009
Reputation Points: 648 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 8 [?]
 
0
 

$PIPESTATUS is an environment variable that holds the return values from any single, or chained commands in an array.... The page here should explain it far better than I can!:
http://www.linuxnix.com/2011/03/pipestatus-internal-variable.html

The && is a logical/boolean AND, similar to C, C++, Java etc.
As far as I understand it, the && inside the braces is part of the compound, boolean expression based on the commands contained in the braces. The && at the end of the line means that the next command (or line) in the bash script will only be executed if the commands to the left of the && (so basically if everything inside the braces) succeeds. So the next line or command in the shell script is the right hand part of the AND at the end of the code you've posted.

So it's kinda like:
If (make 2>&1 OR tee make.log AND exit $PIPESTATUS) AND NextLineOrCommand.....

So it's just a way of conditionally chaining several commands together.
At least I think that's the way it works!

Question Answered as of 1 Year Ago by JasonHippy
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article