Alex Rivera | Logout

What is a simple explanation for how pipes work in Bash?

Asked 2012-03-23T04:13:01.770
131

I often use pipes in Bash, e.g.:

dmesg | less

Although I know what this outputs, it takes dmesg and lets me scroll through it with less, I do not understand what the | is doing. Is it simply the opposite of >?

  • Is there a simple, or metaphorical explanation for what | does?
  • What goes on when several pipes are used in a single line?
  • Is the behavior of pipes consistent everywhere it appears in a Bash script?
Edit
Report

2 Answers

4

The pipe operator takes the output of the first command, and 'pipes' it to the second one by connecting stdin and stdout. In your example, instead of the output of dmesg command going to stdout (and throwing it out on the console), it is going right into your next command.

answered 2012-03-23T04:17:55.973
2
  • | puts the STDOUT of the command at left side to the STDIN of the command of right side.

  • If you use multiple pipes, it's just a chain of pipes. First commands output is set to second commands input. Second commands output is set to next commands input. An so on.

  • It's available in all Linux/widows based command interpreter.

answered 2012-03-23T04:20:07.070

Your Answer