Bash Find out the exit codes of all piped commands

In Bash, you can find out the exit code of each piped command by checking the exit code of the last command in the pipe.

To do this, you can use the special shell variable $? which contains the exit code of the last executed command.

For example, consider the following command:

command1 | command2 | command3

To find the exit code of each command, you can add the following lines after the piped commands:

command1 | command2 | command3

echo "Exit code of command1: $?"
echo "Exit code of command2: $?"
echo "Exit code of command3: $?"

The exit code of each command will be displayed in the output.

Note: The exit code of command1 can only be checked after the entire pipeline of commands has completed execution.

Leave a Comment