Simply put, A pipe redirects the output of the left-hand command to the input of the right-hand command. Simple as that. For example, in the following command.
The Linux creed: "Everything is a file". This includes standard input and output. In Linux, each process gets its own set of files, which gets linked when we invoke piping.
Each process has three default file descriptors:
STDIN is read from your keyboard, while the default for both
STDERR is your screen.
|, operates on the standard output that's coming from the left side, and the input stream that's expected on its right side.
Having this information, we should be now able to understand how to make a script that's able to process data piped to it:
cat /dev/stdin | grep -oP "\d+" || echo "No digits found"
The output of the
cat command will be piped to the input of
grep. If it finds a match, it'll print it to the standard output (your screen), otherwise, we'll echo a string indicating that no match was found.