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
72
votes
14 answers

How to get the PID of a process that is piped to another process in Bash?

I am trying to implement a simple log server in Bash. It should take a file as a parameter and serve it on a port with netcat. ( tail -f $1 & ) | nc -l -p 9977 But the problem is that when the netcat terminates, tail is left behind running.…
Ertuğ Karamatlı
  • 898
  • 1
  • 6
  • 7
71
votes
5 answers

Example of using named pipes in Linux shell (Bash)

Can someone post a simple example of using named pipes in Bash on Linux?
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
71
votes
7 answers

Vim: Pipe selected text to shell cmd and receive output on vim info/command line

I want to pipe the selected text to a shell command and receive the one-line output from this shell command on the vim info/command line? What I'm really trying to do: Pipe the selected text to a pastebin-type shell command and I want to receive the…
ikwyl6
  • 851
  • 1
  • 7
  • 12
71
votes
6 answers

Assigning system command's output to variable

I want to run the system command in an awk script and get its output stored in a variable. I've been trying to do this, but the command's output always goes to the shell and I'm not able to capture it. Any ideas on how this can be done? Example: $…
Sahas
  • 10,637
  • 9
  • 41
  • 51
70
votes
2 answers

Is it possible to get GCC to read from a pipe?

I'm looking for an option to gcc that will make it read a source file from the standard input, mainly so I could do something like this to generate an object file from a tool like flex that generates C code (flex's -t option writes the generated C…
Zifre
  • 26,504
  • 11
  • 85
  • 105
68
votes
4 answers

angular - using async pipe on observable and bind it to local variable in html
Hi I have a observable user$ with a lot of properties (name, title, address...) component{ user$:Observerable; constructor(private userService:UserService){ this.user$ = this.userService.someMethodReturningObservable$() } } Is there…
Han Che
  • 8,239
  • 19
  • 70
  • 116
68
votes
7 answers

Getting head to display all but the last line of a file: command substitution and standard I/O redirection

I have been trying to get the head utility to display all but the last line of standard input. The actual code that I needed is something along the lines of cat myfile.txt | head -n $(($(wc -l)-1)). But that didn't work. I'm doing this on Darwin/OS…
gkb0986
  • 3,099
  • 1
  • 24
  • 22
68
votes
4 answers

read stdin in function in bash script

I have some set of bash functions which output some information: find-modelname-in-epson-ppds find-modelname-in-samsung-ppds find-modelname-in-hp-ppds etc ... I've been writing functions which read output and filter it: function filter-epson { …
likern
  • 3,744
  • 5
  • 36
  • 47
67
votes
6 answers

How to avoid echo closing FIFO named pipes? - Funny behavior of Unix FIFOs

I want to output some data to a pipe and have the other process do something to the data line by line. Here is a toy example: mkfifo pipe cat pipe& cat >pipe Now I can enter whatever I want, and after pressing enter I immediately see the same line.…
user1084871
  • 1,012
  • 1
  • 10
  • 12
66
votes
3 answers

Unix pipe into ls

I thought I understood *nix pipes until now... I have an executable called studio which symlinks to my install of Android Studio and I had assumed I could get the linked-to location with which studio | ls -l But that doesn't work. What it gives me…
hcarver
  • 7,126
  • 4
  • 41
  • 67
66
votes
6 answers

Capture both stdout and stderr in Bash

I know this syntax var=`myscript.sh` or var=$(myscript.sh) Will capture the result (stdout) of myscript.sh into var. I could redirect stderr into stdout if I wanted to capture both. How to save each of them to separate variables? My use case here…
djechlin
  • 59,258
  • 35
  • 162
  • 290
64
votes
9 answers

Why doesn't more Java code use PipedInputStream / PipedOutputStream?

I've discovered this idiom recently, and I am wondering if there is something I am missing. I've never seen it used. Nearly all Java code I've worked with in the wild favors slurping data into a string or buffer, rather than something like this…
Steven Huwig
  • 20,015
  • 9
  • 55
  • 79
64
votes
4 answers

How to make a python script "pipeable" in bash?

I wrote a script and I want it to be pipeable in bash. Something like: echo "1stArg" | myscript.py Is it possible? How?
gbr
  • 1,061
  • 2
  • 9
  • 16
63
votes
4 answers

How to wait for a stream to finish piping? (Nodejs)

I have a for loop array of promises, so I used Promise.all to go through them and called then afterwards. let promises = []; promises.push(promise1); promises.push(promise2); promises.push(promise3); Promise.all(promises).then((responses) => { …
ThePumpkinMaster
  • 2,181
  • 5
  • 22
  • 31
63
votes
3 answers

How to allow html in return of angular2 pipe?

I have a pipe that returns a html string, however the string outputs escaped presumably as a default for security. I'm sure there must be an option to allow html instead but cant find it when I search docs. How can I tell the pipe to allow actual…
rgb
  • 1,246
  • 1
  • 10
  • 14