About
This article talks about the syntax of a Shell Pipeline in Bash
All command(s) in a pipeline are executed:
This page talks about pipeline (e ia command that takes the output of a command as input), a succession of a command in bash is called a list
Syntax
This is the same syntax than a shell pipeline where you can add the time command
[time [-p]] pipeline]
where:
Environment and scope
By default, the environment variables set in a pipeline are scoped to the pipeline and are not passed along to the global scope because it's a subshell.
From bash 4.2+, you can change this behavior with the lastpipe option enabled (may require disabling monitor mode)
shopt -s lastpipe
Test if script is in a pipeline
With bash, the -t test checks if the file descriptor is connected to a terminal. When in a pipeline, stdout is redirected and not connected to a terminal.
if [ -t 1 ]; then
# Not in a pipeline (terminal output), we can use colors
yq file.yaml
exit 0
fi
# in a pipeline, just output the content
cat file.yaml
Execution
(A)synchronous
If the pipeline is not executed asynchronously (see Lists), the shell waits for all commands in the pipeline to complete.
Parallel
Each command in a pipeline is executed as a separate process (i.e., in its own subshell). The shell waits for all commands in the pipeline to terminate before returning a value.
Exit Status
The exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (see The Set Builtin).
If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.
If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status.