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".

  • Can this be solved via xargs?
    – N.N.
    Commented Feb 6, 2013 at 10:47
  • Don't know, but it would not be obvious to me how. As far as I understand xargs would make the input lines from the file string arguments for the command. But I need the opposite.
    – highsciguy
    Commented Feb 6, 2013 at 11:07
  • @rahmu I had a look, but I think the problem setting is a little different there. At least I don't see how the answers would help. The accepted answer to produce temporary files properly is essentially what I wan't to avoid, if not there is some kind of buffering which actually prevents writing the files. I have limited understanding of how the temp files work!
    – highsciguy
    Commented Feb 6, 2013 at 11:12
  • What's wrong with echo $data_are_here | dumb_program?
    – vonbrand
    Commented Feb 6, 2013 at 12:13
  • 1
    This would support only one input file and not all programs would read from stdin.
    – highsciguy
    Commented Feb 6, 2013 at 12:42

3 Answers 3


In Bash, you can use the command1 <( command0 ) redirection syntax, which redirects command0's stdout and passes it to a command1 that takes a filename as a command-line argument. This is called process substitution.

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 wdiff:

user@host:/path$ wdiff <( echo hello; echo hello1 ) <( echo hello; echo hello2 )

In the background, this creates a FIFO, pipes the command inside the <( ) to the FIFO, and passes the FIFO's file descriptor as an argument. To see what's going on, try using it with echo to print the argument without doing anything with it:

user@host:/path$ echo <( echo hello )

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 >( ) syntax for when you want to use it as output, e.g.

$ someprogram --logfile >( gzip > out.log.gz )

See also the bash man page "process substitution" section and the Bash redirections cheat sheet for related techniques.

  • This is not supported in KSH Commented Jul 3, 2014 at 9:58
  • 8
    ksh invented this. You're using a variant of ksh that that doesn't support it Commented Jul 31, 2016 at 1:28
  • 5
    Some programs that take filename command-line arguments actually need a real random-access file, so this technique won't work for those. What do you do in these cases. For example ssh -F <(vagrant ssh-config) default would be really nice but alas.
    – Sukima
    Commented Nov 9, 2017 at 22:44

Use a named pipe. By way of illustration:

mkfifo fifo
echo -e "hello world\nnext line\nline 3" > fifo

The -e tells echo to properly interpret the newline escape (\n). This will block, ie, your shell will hang until something reads the data from the pipe.

Open another shell somewhere and in the same directory:

cat fifo

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 echo.

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.

  • Ok, thanks, this works also with two named pipes and wdiff. But I thought to understand that there is a certain (small) amount of memory available for the pipe as buffer. What happens if I exceed the buffer size?
    – highsciguy
    Commented Feb 6, 2013 at 11:57
  • I added a final paragraph about that issue.
    – goldilocks
    Commented Feb 6, 2013 at 12:19
  • 3
    /tmp is configure in most distros to use a tmpfs filesystem which is in RAM. When you write a file in /tmp it goes directly to your RAM which makes is a good answer for semi-resilient files that have to be accessed quickly and rewritten many times.
    – user21228
    Commented Feb 7, 2013 at 9:57
  • Remember to rm fifo when you're done! I just drew my sysadmins' ire by leaving an empty named pipe in my homedir, and their file sync software hung trying to read it.
    – Jacktose
    Commented Jun 12 at 18:51
  • I'd question your sysadmin's judgement in using file sync software that doesn't bother to check file types before it tries to read them.
    – goldilocks
    Commented Jun 13 at 13:56

wdiff is a special case due to it requiring 2 filename arguments, but for all commands that only require 1 argument and which stubbornly refuse to take anything but a filename argument, there are 2 options:

  • The filename '-' (that is, a minus sign) works about 1/2 of the time. It seems to depend upon the command in question and whether the developer of the command traps that case and handles it as expected. e.g.

    $> ls | cat -

  • There is a psuedo file named /dev/stdin that exists in linux and can be used were a filename is absolutely required by a command. This is more likely to work since it is does not require any special filename handling from the command. If a fifo works, or the bash process substitution method works then this should also work and is not shell specific. e.g.

    $> ls | cat /dev/stdin

  • 1
    less and openssl like /dev/stdin rather than /dev/fd/NUM :-)
    – eel ghEEz
    Commented Apr 30, 2016 at 0:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .