Shell – Chaining pipe | with &&

exit-statuspipeshell

I have this command where I want to filter make output:

cd /app && make && sudo nginx -g 'daemon off;'

What is the correct way to insert make | pv -q -L 100 here?

Best Answer

The problem is that you'll be checking the exit status of pv. With POSIX sh syntax, you could do:

cd /app && ((make 3>&- && exec sudo nginx -g 'daemon off;' >&3 3>&-) | pv -qL 100) 3>&1

Or with ksh/bash/zsh:

(set -o pipefail
cd /app && make | pv -qL 100 && sudo nginx -g 'daemon off;')

Or with zsh:

cd /app && make | pv -qL 100 && ((!pipestatus[1])) && sudo nginx -g 'daemon off;'

Or with bash:

cd /app && make | pv -qL 100 && ((!PIPESTATUS[0])) && sudo nginx -g 'daemon off;'
Related Question