2

I want to call ./prog with arguments from args.txt, but meanwhile, ./prog also reads from file input in.txt

I tried cat args.txt | xargs ./prog < in.txt but it doesn't work.

I understand if the program doesn't need input, cat args.txt | xargs ./prog should work. But in this case, how should I write it..?

Thank you in advance.

1
  • ./prog $(cat args.txt) won't work if args.txt have special characters. for example, I might want to pass contents: "arg1 arg2" arg3 as 2 arguments, where the 1st argument is "arg1 arg2" as a whole, the 2nd is arg3 Commented Sep 26, 2014 at 0:52

1 Answer 1

3

GNU xargs has a -a option to pass in arguments from a file.

xargs -a args.txt ./prog < in.txt

If your xargs doesn't have -a, you could try one of these alternatives:

./prog $(< args.txt) < in.txt

cat args.txt | xargs bash -c './prog "$@" < in.txt' --

The second one is ugly but also more robust when given arguments with whitespace, particularly if you use xargs -0.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the nice and clean answer. ./prog $(< args.txt) < in.txt works for me. Could you explain the difference between this and ./prog $(cat args.txt) < in.txt? Does '<' also read file contents to stdout, so that the contents are caught by $()? Isn't '<' an input redirection?
$(<file) is a shorter way of writing $(cat file). Basically no difference. Use cat if you find that more readable.
I see. Then I might consider the 2nd solution since cat file won't work if the contents have special characters like whitespaces

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.