theshell.ch/site/pages/cmd/advanced/pipes.html

43 lines
1.1 KiB
HTML

---
layout: page
author: Domenico Votta
category-page: advanced
category-title: Advanced commands
tags: pipes redirect output input
title: Pipes
previous-page: pages/cmd/advanced/paste.html
next-page: pages/cmd/advanced/tr.html
---
The <code>pipes</code>, which in the shell are represented with the symbol | ,
are used to join two commands on the terminal, taking the output of the first
command and using it as input of the second.<br>
It is common to see the commands <code>grep</code> and <code>ps</code> together,
an example below.<br>
Example: lists all processes with your username
<pre>
ps aux | grep user
</pre>
Example: count the lines of a file, using the
<a href="{{ site.baseurl }}/pages/cmd/advanced/nl.html">nl</a> command
<pre>
cat example1.txt | nl
1 Car
2 Computer
3 Robot
4 Smartphone
5 Videogame
</pre>
Example: passing expressions to <a href="{{ site.baseurl }}/pages/cmd/advanced/bc.html">bc</a>
to do calculations
<pre>
echo "(12 / 2 - 3) * 3 + 1.5" | bc
10.5
</pre>
You can use the commands that you learned and put them together, always
respecting the logic of the command.