AnyCompiler

Command Substitution and Pipes

| sends one command's output directly into the next command's input, without any intermediate variable — this is how Bash composes small, single-purpose commands into a larger pipeline.

echo -e "banana\napple\ncherry" | sort
Output
apple
banana
cherry

Wrapping an entire pipeline in $(...) captures its final output into a variable, the same command-substitution syntax from the quoting lesson — it works on a pipeline exactly as it does on a single command.

sorted=$(echo -e "banana\napple\ncherry" | sort)
echo "$sorted"
Output
apple
banana
cherry

Next: redirection and heredocs — sending output to a file or discarding it, and writing multi-line text inline.

Practice exercise

Pipe the lines "banana", "apple", "cherry" (one per line, via echo -e) through sort, capture the sorted result into a variable with command substitution, and print that variable.