Search code examples
linuxpipecommand-line-argumentscatargv

How to pass command line parameters from a file


I have a C program that reads command line arguments from argv. Is it possible to make a pipe to redirect the contents of a file as command line arguments to my program? Suppose I have a file arguments.dat with this content:

0 0.2 302 0

And I want my program to be called with:

./myprogram 0 0.2 302 0

I tried the following:

cat arguments.dat | ./myprogram

without success.


Solution

  • With most shells, you can insert the contents of a file into a command line with $(<filename):

    ./myprogram $(<arguments.dat)
    

    If your shell doesn't support that, then one of the older ways will work:

    ./myprogram $(cat arguments.dat)
    ./myprogram `cat arguments.dat`   # need this one with csh/tcsh
    

    (You do know the difference between command line arguments and file input, right? Why would you expect to pipe command line arguments into a program?)