How to pass quoted args to GNU Parallel

chaimp picture chaimp · Nov 22, 2011 · Viewed 8.1k times · Source

I need to pass some text that includes whitespace and other characters to a script that's being run by GNU Parallel.

Here is a very simple example:

$ seq 1 3 | parallel echo "Quoted ' (text)"

The above example will output this:

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file    

However, if I do this everything works:

seq 1 3 | parallel echo "\"Quoted ' (text)\""

I happen to be running this from a python script, so before passing the arguments I'm double quoting them in the script like this:

args = ["Some arg", "Another arg", "etc."]
args = ' '.join(pipes.quote(pipes.quote(arg)) for arg in args)

But it doesn't seem like a clean solution.

Does anybody know of a better way to pass arguments to GNU Parallel?

Thanks!

Answer

Dimitre Radoulov picture Dimitre Radoulov · Nov 22, 2011
zsh-4.3.12[sysadmin]% print -l {1..3} | 
  parallel -q echo "Quoted ' (text)"
Quoted ' (text) 1
Quoted ' (text) 2
Quoted ' (text) 3

As described by @mortehu:

Arguments passed to commands through parallel are expanded by the shell twice: once in the invocation of parallel, and once when parallel runs your command. -q prevents the second shell expansion.