How do I use the lines of a file as arguments of a command?

Yoo picture Yoo · Nov 19, 2010 · Viewed 170.8k times · Source

Say, I have a file foo.txt specifying N arguments

arg1
arg2
...
argN

which I need to pass to the command my_command

How do I use the lines of a file as arguments of a command?

Answer

glenn jackman picture glenn jackman · Nov 19, 2010

If your shell is bash (amongst others), a shortcut for $(cat afile) is $(< afile), so you'd write:

mycommand "$(< file.txt)"

Documented in the bash man page in the 'Command Substitution' section.

Alterately, have your command read from stdin, so: mycommand < file.txt