I often want to feed relatively short string data (could be several lines though) to commandline programs which accept only input from files (e.g. wdiff) in a repeated fashion. Sure I can create one or more temporary files, save the string there and run the command with the file name as parameter. But it looks to me as if this procedure would be highly inefficient if data is actually written to the disk and also it could harm the disk more than necessary if I repeat this procedure many times, e.g. if I want to feed single lines of long text files to wdiff. Is there a recommended way to circumvent this, say by using pseudo files such as pipes to store the data temporarily without actually writing it to the disk (or writing it only if it exceeds a critical length). Note that wdiff takes two arguments and, as far as I understand it will not be possible to feed the data doing something like wdiff <"text".
|
|
|||
| show 3 more comments |
|
Use a named pipe. By way of illustration:
The Open another shell somewhere and in the same directory:
You'll read the echo, which will release the other shell. Although the pipe exists as a file node on disk, the data which passes through it does not; it all takes place in memory. You can background ( The pipe has a 64k buffer (on linux) and, like a socket, will block the writer when full, so you will not lose data as long as you do not prematurely kill the writer. |
|||||||
|
|
In Bash, you can use the Some programs that take filename command-line arguments actually need a real random-access file, so this technique won't work for those. However, it works fine with
In the background, this creates a FIFO, pipes the command inside the
Creating a named pipe is more flexible (if you want to write complicated redirection logic using multiple processes), but for many purposes this is enough, and is obviously easier to use. There's also the
See also the Bash redirections cheat sheet for related techniques. |
|||
|
|


xargs? – N.N. Feb 6 at 10:47xargswould make the input lines from the file string arguments for the command. But I need the opposite. – highsciguy Feb 6 at 11:07echo $data_are_here | dumb_program? – vonbrand Feb 6 at 12:13