Read file lines into shell line separated by space

I have a file called requirements.txt, each line has a package :

package1
package2
package3

I'm looking for a command that will take each line of requirements.txt and join them in one line, space separated.

So basically, I'm looking to generate the following command:

command package1 package2 package3

I tried using a for and applying command for each line of requirements.txt but it was much slower


You can use xargs, with the delimiter set to newline (\n): this will ensure the arguments get passed correctly even if they contain whitespace:

xargs -rd'\n' command < requirements.txt

From man page:

-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.

--delimiter=delim, -d delim
Input items are terminated by the specified character.


You can simply use bash redirection and command substitution to get the file contents as arguments to your command:

command $(<file)

This works because not only space is a valid word splitting character, but newline as well – you don’t need to substitute anything. However, in the case of many lines you will get an error referring to the shell’s ARG_MAX limit (as you will with substitution solutions). Use printf to built a list of arguments and xargs to built command lines from it to work around that:

printf "%s\0" $(<file) | xargs -0 command

Note that neither of these approaches work for lines containing whitespace or globbing characters (besides the newline of course) – fortunately package names don’t (AFAIK). If you have whitespace or globbing characters in the lines use either xargs (see this answer) or parallel (see this answer).