37

let's say I want to run ./program with a string argument

instead of typing ./program string each time, how can I do ./program <file> where <file> is a file that contains string?

thankyou

6 Answers 6

62

This should do the trick:

./program `cat file`

If you want the entire file content in a single argument, add double quotation (tested in bash. I think It may vary shell to shell) :

./program "`cat file`" 
2
41

Command

./program "$(< file)"

Explanation

  1. $(cmd) runs a command and converts its output into a string. $(cmd) can also be written with backticks as `cmd`. I prefer the newer $(cmd) as it nests better than the old school backticks.

  2. The command we want is cat file. And then from the bash man page:

    The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

  3. The quotes around it make sure that whitespace is preserved, just in case you have spaces or newlines inside of your file. Properly quoting things in shell scripts is a skill that is sorely lacking. Quoting variables and command substitutions is a good habit to good into so you don't get bit later when your file names or variable values have spaces or control characters or other weirdnesses.

0
6

You can use one of:

./program $(cat file)
./program "$(cat file)"

The latter is useful if there may be multiple words in the file and you want them to be treated as a single argument. In any case, I prefer the use if $() to backticks simply due to it's ability to nest (which is probably not a requirement in this case).

Also keep in mind that this answer (and others) are more related to the shell in use rather than Linux itself. Since the predominant and the best :-) shell seems to be bash, I've coded specifically for that.

This is actually a fairly common way of killing processes under UNIX lookalikes. A daemon (like cron) will write its process ID to a file (like /var/cron.pid) and you can signal it with something like:

kill -HUP $(cat /var/cron.pid)
5

Another option is xargs:

xargs -a file ./program

From the man page:

xargs reads items from the standard input, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input.

In plain speak, xargs will execute the command adding the content of file as additional arguments, separating them by spaces or newlines.

See man xargs for more options.

2
  • 4
    Even better, drop the cat and use xargs's -a <file> option, which will read items from file instead of standard input.
    – jimmyorr
    Oct 16, 2012 at 19:19
  • "xargs will execute ./program on each line/word that is being piped to it.": this is incorrect. xargs will pass all arguments to the command unless they exceed either the command's allowed limit or a self-defined limit. only in that case it will split the arguments between multiple commands to make sure all of them were used. I will edit it out.
    – gilad905
    Jan 9 at 15:30
2

All the above answers work but I find the cleanest and easiest to type option is

xargs < file ./program
1

This is extension to @robbrit answer.

Consider a file.txt with content:

1 2 3
4 5 6

And the a program.c with content:

#include<stdio.h>
void main(int argc, char** argv) 
{ 
    for (int i = 0; i < argc; i++) 
        printf("Arg %d: %s\n", i, argv[i]); 
}

Here is what happen if you pass command line arguments with quotes and without:

$ gcc  program.c -o program

$ ./program `cat file`
Arg 0: ./program
Arg 1: 1
Arg 2: 2
Arg 3: 3
Arg 4: 4
Arg 5: 5
Arg 6: 6

$ ./program "`cat file`"
Arg 0: ./program
Arg 1: 1 2 3
4 5 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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