Questions tagged [pipe]

A pipe is an interprocess connection between file descriptors of two processes. A pipe is created with the POSIX pipe() function (from ). Shells create pipes between processes if the "|" symbol is used: "cmd1 | cmd2" directs the output of cmd1 to the input of cmd2. On Windows use CreatePipe(). This mechanism redirects standard input, standard output, and standard error into the calling process in .NET and Java.

Every program that runs on the command line has three data streams connected to it: STDIN(0) - standard input, STDOUT (1) - standard output - data printed by the program, STDERR(2) - standard error.

There are ways to connect streams between programs and files called piping and redirections.

Piping is a mechanism for sending data from one program to another using the "|" operator. The operator feeds the output from the program on the left as input to the program on the right.

Example

$ cat two_columns
column1:cloth
column2:strawberries
column3:fish
column4:chocolate
column5:punch cards
$ cat two_columns | awk -F: '{print $1}'
column1
column2
column3
column4
column5 
$ cat two_columns | awk -F: '{print "HAS: " $2}'
HAS: cloth
HAS: strawberries
HAS: fish
HAS: chocolate
HAS: punch cards 

Useful Links

9809 questions
119
votes
5 answers

How do you catch error codes in a shell pipe?

I currently have a script that does something like ./a | ./b | ./c I want to modify it so that if any of a, b, or c exit with an error code I print an error message and stop instead of piping bad output forward. What would be the simplest/cleanest…
hugomg
  • 68,213
  • 24
  • 160
  • 246
118
votes
7 answers

How to send a simple string between two programs using pipes?

I tried searching on the net, but there are hardly any resources. A small example would suffice. EDIT I mean, two different C programs communicating with each other. One program should send "Hi" and the other should receive it. Something like that.
user244333
117
votes
8 answers

How can I get a list of all open named pipes in Windows?

Is there an easy way to test whether your named pipe is working correctly? I want to make sure that the data I'm sending from my app is actually being sent. Is there a quick and easy way to get a list of all the named pipes?
Doug T.
  • 64,223
  • 27
  • 138
  • 202
115
votes
3 answers

Pipe subprocess standard output to a variable

I want to run a command in pythong, using the subprocess module, and store the output in a variable. However, I do not want the command's output to be printed to the terminal. For this code: def storels(): a =…
Insomaniacal
  • 1,907
  • 3
  • 14
  • 10
115
votes
5 answers

How to pipe input to a Bash while loop and preserve variables after loop ends

Bash allows to use: cat <(echo "$FILECONTENT") Bash also allow to use: while read i; do echo $i; done
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
115
votes
9 answers

How to execute the output of a command within the current shell?

I'm well aware of the source (aka .) utility, which will take the contents from a file and execute them within the current shell. Now, I'm transforming some text into shell commands, and then running them, as follows: $ ls | sed ... | sh ls is just…
kch
  • 77,385
  • 46
  • 136
  • 148
115
votes
8 answers

use pipe for curl data

I'm trying to pass the cat output to curl: $ cat file | curl --data '{"title":"mytitle","input":"-"}' http://api But input is literally a -.
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
113
votes
11 answers

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

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…
Village
  • 22,513
  • 46
  • 122
  • 163
113
votes
10 answers

Split output of command by columns using Bash?

I want to do this: run a command capture the output select a line select a column of that line Just as an example, let's say I want to get the command name from a $PID (please note this is just an example, I'm not suggesting this is the easiest…
flybywire
  • 261,858
  • 191
  • 397
  • 503
110
votes
2 answers

Piping buffer to external command in Vim

I would like to send contents of the current buffer to stdin of external command (like mail). How do I send a Vim buffer to an external command?
yasar
  • 13,158
  • 28
  • 95
  • 160
105
votes
5 answers

How to declare a pipe globally to use in different modules?

I have a custom pipe named CurrConvertPipe import {Pipe, PipeTransform} from '@angular/core'; import {LocalStorageService} from './local-storage'; @Pipe({name: 'currConvert', pure: false}) export class CurrConvertPipe implements PipeTransform { …
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
103
votes
8 answers

How to pipe several commands in Go?

How can I pipe several external commands together in Go? I've tried this code but I get an error that says exit status 1. package main import ( "io" "log" "os" "os/exec" ) func main() { c1 := exec.Command("ls") stdout1, err…
user1243746
101
votes
7 answers

Node.js Piping the same readable stream into multiple (writable) targets

I need to run two commands in series that need to read data from the same stream. After piping a stream into another the buffer is emptied so i can't read data from that stream again so this doesn't work: var spawn =…
Maroshii
  • 3,937
  • 4
  • 23
  • 29
100
votes
8 answers

Pipe output to bash function

I have as simple function in a bash script and I would like to pipe stdout to it as an input. jc_hms(){ printf "$1" } I'd like to use it in this manner. var=`echo "teststring" | jc_hms` Of course I used redundant functions echo and printf to…
jliu83
  • 1,553
  • 5
  • 18
  • 23
97
votes
1 answer

gdb - debugging with piped input (not arguments)

I typically run my program with: perl -e 'print "A"x200' | ./vuln_prog The stdin is consumed by a gets() function in C++. If this were just a command argument, I could open gdb by doing: gdb ./vuln_prog run $(perl -e 'print "A"x200') However, my…
mandreko
  • 1,766
  • 2
  • 12
  • 24