Command Line for a Web Developer: Piping

27 May, 2018
  • Share:

What is Piping?

Before diving into advanced command line usage examples, I'd like to explain another basic concept - piping. Piping is the inter-process communication mechanism that allows to send the output of one program to another program. To pipe programs together in a shell, we use the vertical bar character |. The general syntax is:

command one | command two [| command three …]

What happens when you pipe 2 programs?

The shell creates a process for each program and arranges the necessary connections between their standard streams. Basically, the stdout from the program on the left is directed to the stdin of the program on the right through a pipe.

Finding Ids of Google Chrome Processes

Let's go through a simple example. We'll pipe 4 commands to find the ids of all running Google Chrome processes:

ps aux | grep "Google Chrome" | grep -v "grep" | awk '{print $2}'
  1. First, we run ps aux to get the list of all running processes in the table format.
  2. Next, we use grep Google Chrome to see only the lines which contain the "Google Chrome" string.
  3. Then, we run grep -v "grep" to exclude all grep processes from the table (in particular, grep "Google Chrome", which we ran in the previous step).
  4. Finally, because we need just the process id (which is the column #2 in the table), we use the awk '{print $2}' command to print only the second column of the table.

Summary

To conclude, we use the | (pipe) operator to feed the output of one program as input to another program. We can pipe multiple programs together to customize the output however we like.

  1. Pipeline_(Unix)
  2. Pipes: A Brief Introduction