Why does the parallel command not work with backslashes when taking input from file?

Viewed 111

Consider the following file saved as commands.txt

ls \
&& pwd

ls \
&& pwd

Now,

bash commands.txt

works as expected to give

LICENSE
/home/username/utilities
LICENSE
/home/username/utilities

but

parallel < commands.txt

gives the error

/bin/bash: -c: line 0: syntax error near unexpected token `&&'
/bin/bash: -c: line 0: `&& pwd'
ls: cannot access '\': No such file or directory
/bin/bash: -c: line 0: syntax error near unexpected token `&&'
/bin/bash: -c: line 0: `&& pwd

Why do multiple lines with the same command separated by \ not seem to work with parralel as such?

2 Answers

Why do multiple lines with the same command separated by \ not seem to work with parralel as such?

Because parallel does not parse \ and executes a separate shell for each line.

If your input has \n\n after each group (and only there), you can do:

cat commands.txt | parallel -d '\n\n' 
Related