This question already has an answer here:

I'm trying to figure out how I can send arguments (which are contents in a file) on the command line as parameters to a cpp function. I am NOT trying to send it through stdin.

I have a function named foo.cpp:

  $ cat foo.cpp
  #include <iostream>
  int main (int argc, char *argv[]) {
      for (int i = 1; i < argc; i++)
          std::cout << argv[i] << std::endl;
      return 0;
  $ g++ -o foo foo.cpp
  $ ./foo hello world

In the last command executed, I'm passing the arguments "hello world" into the program. However, what if I have "hello world" in a file? For example:

 $ cat footest.args
 $ hello world

How would I pass the contents of footest.args into ./foo as above? I've tried

  ./foo | cat footest.args

But it doesn't seem to be working. What am I doing wrong?

marked as duplicate by muru, dr01, Anthon, taliezin, John WH Smith Oct 2 '15 at 9:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You want to take output of a command and transform it into arguments. Try command substitution:

./foo $(< footest.args)

You can achieve what you want with:

cat footest.arg | xargs ./foo

or (thanks to @glenn)

xargs ./foo < footest.arg

avoiding one unnecessary use of cat.

  • 1
    or xargs ./foo < footest.arg – glenn jackman Oct 1 '15 at 16:18

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