Technical skill

Bash syntax: Pipelines

Pass output along a chain

Imagine sorting fruit names into groups, then counting how many of each name appear. A Bash pipeline connects small commands so the first command's output becomes the next command's input. Each command handles one step, and the final output shows the result. The first command writes to standard output; the next reads that data as standard input:

printf '%s\n' pear apple pear | sort | uniq -c

Here printf prints three names, sort puts matching names together, and uniq -c counts each run of identical lines. A pipe carries bytes, usually text; it does not turn text into structured records. Spaces, newlines, and character encodings affect what text tools see.

Keep results and errors separate

Bash connects each pair of neighboring commands with a pipe. A command's ordinary output is called standard output; its error messages go to a separate stream called standard error. Standard error stays separate by default, so an error from the first command does not become data for the next one. Bash's |& operator also sends standard error through the pipe; it is shorthand for 2>&1 |. That can help when you want to examine diagnostics. Mixing them into ordinary output can confuse a later command that expects only data.

Pipeline status affects how a script decides whether a step worked. By default, Bash reports the exit status of the last command. If grep finds no match but sort succeeds, the pipeline can report success even though no line matched. Enable pipefail when any failed stage should make the pipeline fail:

set -o pipefail
grep -F 'ready' ./service.log | sort | uniq -c
status=$?
printf 'pipeline status: %s\n' "$status"

With pipefail, Bash returns the status of the rightmost command that returned a nonzero status, or zero if all commands succeeded. Nonzero does not always mean something broke: grep uses status 1 when it finds no matches, which may be the expected result of a search. Decide how your script should treat it. Save $? immediately because the next command replaces it.

Remember where a loop runs

Bash normally runs each pipeline component in a separate subshell, a child shell with its own copy of variables. A variable changed inside a loop that is the last pipeline component will not usually change the parent shell's copy:

count=0
printf '%s\n' a b | while IFS= read -r item; do
  count=$((count + 1))
done
printf '%s\n' "$count"

In the usual execution mode, the final output is 0. Bash's lastpipe option is an exception: with job control inactive, Bash may run the final component in the current shell. If later code needs the loop's value, use a structure that keeps the loop in that shell or account for lastpipe explicitly.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful