I want to execute a command (say ls) and sed its output, then save the stdout to a variable, like this,
OUT=$(ls | sed -n -e 's/regexp/replacement/p')
After this, if I try to access the $PIPESTATUS array, I get only 0 (which is same as $?). So, how can I get both $PIPESTATUS as well as capture the entire piped command's stdout?
Note:
- If I only executed those piped commands and didn't capture the stdout (like
ls | sed -n -e 's/regexp/replacement/p'), I get expected exit statuses in$PIPESTATUS(like0 0) - If I only executed single command (without piping multiple commands) using Command Substitution and captured the stdout (like
OUT=$(ls)), I get expected single exit status in$PIPESTATUS(which is same as$?)
P.S. I know, I could run the command 2 times (first to capture the stdout, second to access $PIPESTATUS without using Command Substitution), but is there a way to get both in single execution?