PIPESTATUS
In the Bash shell, PIPESTATUS is a special array variable that stores the exit statuses of commands executed in a pipeline. This feature allows users to inspect the success or failure of individual commands within a sequence of piped operations. For example, when a pipeline of commands is executed, PIPESTATUS captures the exit status of each command in the order they were run, enabling more granular error checking and control flow management.
Usage Examples
A simple demonstration of PIPESTATUS in action involves executing a pipeline and then inspecting its exit statuses:
```bash $ cat /etc/passwd | rev | false $ echo ${PIPESTATUS[@]} 0 141 1 ```
In this example: - `cat /etc/passwd` reads the contents of the `/etc/passwd` file. - `rev` reverses the lines of input. - `false` deliberately returns a non-zero exit status (141 in this case). - The `echo ${PIPESTATUS[@]}` command outputs the exit statuses of each command in the pipeline: `0` (success for `cat`), `141` (failure for `false`), and `1` (failure for `rev`, which inherits the exit status of the previous command in the pipeline).
To check whether any command in a pipeline failed, the following pattern can be used:
```bash [[ -n ${PIPESTATUS[*]/0/} ]] && echo "Error Will Robinson!" ```
This command evaluates the PIPESTATUS array, replacing all `0` (success) values with empty strings. If the resulting array is non-empty (indicating at least one non-zero exit status), the message "Error Will Robinson!" is printed.
Technical Details
The PIPESTATUS array is specific to Bash and is not available in other shells like Zsh or Dash. Each element in the array corresponds to the exit status of a command in the most recently executed pipeline. For instance, in a pipeline like `command1 | command2 | command3`, `PIPESTATUS[0]` holds the exit status of `command1`, `PIPESTATUS[1]` holds the exit status of `command2`, and `PIPESTATUS[2]` holds the exit status of `command3`.
This behavior is particularly useful for debugging complex command sequences or implementing robust error-handling logic in shell scripts. However, it is important to note that PIPESTATUS only retains the exit statuses of the last executed pipeline. Subsequent commands or pipelines will overwrite the array.
This page contains information generated by Ollama. The information was reviewed, and may have been altered, by a human editor before being added to the Featured category. As of July 2026 information generated by AI is not subject to copyright and is thus in the public domain. This page is pending human review.