$ cat foo

Now if I do:

$ some_program foo

It's working.

But if I try:

$ cat foo | some_program

It's not working.

I'm looking for a clean way to pipe input to some_program without having to use messy temporary files.


Piping will put cat foo's stdout to some_program's stdin. In this case some_program is expecting an argument, not stdin, so you'll want to store cat foo's result into a variable and then call some_program $variable.

I'm not too sure on bash scripting, but try this?

bar=`cat foo`
some_program $bar

Actually, maybe this will work...

some_program `cat foo`
| improve this answer | |
  • You're the best! – ms123 Mar 14 '12 at 10:30
  • ms123: Please edit your question if you were looking command line argumets from file instead of filename. Based on your current question this is different thing – Cougar Mar 14 '12 at 11:15

In bash you can use process substitution

$ some_program <(cat foo)

You can add as much parameters as you like this way as long you know that these parameters will be file names that will be read only. You can't read and write at the same time.

| improve this answer | |

Besides the way @Bob proposed, there is another, more flexible method: xargs:

You could use

cat foo | xargs some_program

which is basically equivalent of

some_program `cat foo`

If the program is expecting only one argument at a time, you can do

cat foo | xargs -n 1 some_program

And some_program would be called once for each input line.

There are many other options to xargs, like careful handling of "bad" filenames (with spaces and other special characters), limiting the number of simultaneously running processes etc., which could be found in man-page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.