STDIN of Following Command
The Bash pipe operator
| provides the abililty to take the
STDOUT from the first command and use it as the
STDIN for the next command.
The pipe operator is a powerful tool in Bash that allows us to create specific and sometimes complex commands.
The pipe operator syntax will look similar to:
[bash-command-one] | [bash-command-two].
Whatever contents added to
[bash-command-one] will be used as the
ls -a to
ls -a | less
Upon executing the preceding command the
ls -a will be used as the
STDIN for the
less command. This will open up the
less view window in the terminal.
The picture displays a
less window. The contents can be navigated using the
less tools, and can be ultimately exited with a press of the
The output you see may be slightly different than the output from the picture. Many of the hidden files and directories in the picture will eventually find their way onto your machine as your continue through this course.
history of your bash terminal is useful, but can be overwhelming:
STDOUT of the
history command defaulted to the terminal window. It would be much easier to view the contents in a
less window. This can be achieved by piping
history to the
history | less
Your history will be different than the above picture. You can exit the
less window by pressing the
grep is a searching tool that we will learn about in a future article, but is a great example of how the
bash pipe operator can be used.
In this case the
history needs to be filtered to only include
ls commands. This can be achieved by executing
history and piping the
STDOUT to the
grep command with an argument searching for only
history | grep 'ls'
bash you can chain together multiple commands with the pipe operator to create specific and sometimes complex commands.
Suppose you wanted to open the
history | grep 'ls' in
less you can add another pipe:
history | grep 'ls' | less
STDOUT contents of
history | grep 'ls' have now been passed as the
STDIN to the
You can chain as many pipes together as you need creating complex, but simple to understand, pipelines.