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
28
votes
8 answers

Angular 5 - currencyPipe

I have a problem with the already built in CurrencyPipe from Angular. I need to display a currency sign using the CurrencyPipe but I can't use it unless I provide an input number. Because the CurrencyPipe uses the current locale to get the currency…
David Casanellas
  • 815
  • 2
  • 11
  • 18
28
votes
3 answers

equivalent of pipefail in GNU make?

Say I have the following files: buggy_program: #!/bin/sh echo "wops, some bug made me exit with failure" exit 1 Makefile: file.gz: buggy_program | gzip -9 -c >$@ Now if I type make, GNU make will happily build file.gz even though buggy_program…
unhammer
  • 4,306
  • 2
  • 39
  • 52
27
votes
8 answers

Can someone explain what dup() in C does?

I know that dup, dup2, dup3 "create a copy of the file descriptor oldfd"(from man pages). However I can't digest it. As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
27
votes
7 answers

Why does ps o/p list the grep process after the pipe?

When I do $ ps -ef | grep cron I get root 1036 1 0 Jul28 ? 00:00:00 cron abc 21025 14334 0 19:15 pts/2 00:00:00 grep --color=auto cron My question is why do I see the second line. From my understanding, ps lists the…
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
27
votes
5 answers

How to pipe input to Java program with bash

My Java program is listening on standard input: InputStreamReader isReader = new InputStreamReader(System.in); BufferedReader bufReader = new BufferedReader(isReader); while(true){ try { String inputStr = null; …
Chetan
  • 46,743
  • 31
  • 106
  • 145
27
votes
2 answers

Need some explaination of kubectl stdin and pipe

I'm daily user of kubectl, but not expert of linux. Recently I need edit some service type after deployment, so searched and used kubectl replace and it worked well. cat yaml | kubectl replace -f - service/tracs-pool-1sv replaced But I don't…
Lei Yang
  • 3,970
  • 6
  • 38
  • 59
27
votes
2 answers

How to diff file and output stream "on-the-fly"?

I need to create a diff file using standard UNIX diff command with python subprocess module. The problem is that I must compare file and stream without creating tempopary file. I thought about using named pipes via os.mkfifo method, but didn't reach…
Enchantner
  • 1,534
  • 3
  • 20
  • 40
27
votes
6 answers

OS X / Linux: pipe into two processes?

I know about program1 | program2 and program1 | tee outputfile | program2 but is there a way to feed program1's output into both program2 and program3?
Jason S
  • 184,598
  • 164
  • 608
  • 970
26
votes
2 answers

How to pipe Bash Shell command's output line by line to Perl for Regex processing?

I have some output data from some Bash Shell commands. The output is delimited line by line with "\n" or "\0". I would like to know that is there any way to pipe the output into Perl and process the data line by line within Perl (just like piping…
user1129812
  • 2,859
  • 8
  • 32
  • 45
26
votes
5 answers

How to tell if the output of the "find" command is empty?

I want to return an exit status of 0 if the output is empty and 1 otherwise: find /this/is/a/path/ -name core.*
cstack
  • 2,152
  • 6
  • 28
  • 47
26
votes
2 answers

Piping data to Linux program which expects a TTY (terminal)

I have a program in Linux which refuses to run if its stdin/stdout is not a TTY (terminal device). Is there an easy-to-use tool which will create a PTY, start the program with the newly created TTY, and copy all data over stdin/stdout? The use case…
pts
  • 80,836
  • 20
  • 110
  • 183
26
votes
3 answers

Piping stdout and stderr inside a Makefile rule

I want to pipe the output of a script to a different program. Something I would normally do using these two forms: python test.py 2>&1 | pyrg python test.py |& pyrg My problem is that it doesn't work from inside a makefile: [Makefile] test: …
Xyand
  • 4,470
  • 4
  • 36
  • 63
26
votes
3 answers

ruby pipe operator

I'm new to ruby, and I saw this code snippet 1|2 and it returns 3 What does the | operator actually do? I couldn't seem to find any documentation on it. Also, in this context is it referred to as the "pipe" operator? or is it called something else?
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
25
votes
9 answers

Is there a command-line shortcut for ">/dev/null 2>&1"

It's really annoying to type this whenever I don't want to see a program's output. I'd love to know if there is a shorter way to write: $ program >/dev/null 2>&1 Generic shell is the best, but other shells would be interesting to know about too,…
Steve
25
votes
5 answers

Shell pipe: Exit immediately when one command fails

I'm using a pipe of several commands in bash. Is there a way of configuring bash to terminate all commands in the whole pipeline immediately should one of the commands fail? In my case, the first command, say command1, runs for a while until it…
Tobi
  • 253
  • 1
  • 3
  • 6