4

I'm trying to get parallel to run a command and pass a date in quotes. But it's adding an extra backslash before the spaces in the parameter:

$ echo "2014-05-01 01:00" | parallel --dry-run foo \"{}\"
foo "2014-05-01\ 01:00"

How can I get parallel to not add that extra \ after 2014-05-01?

Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98

1 Answers1

4

Drop the escaped quotes from your command:

echo "2014-05-01 01:00" | parallel --dry-run foo "{}"

When parallel replaces {} with the line of input, it knows that the line should be a single argument, so it will escape the space for you. Your previous command simply added literal quote characters to the beginning and end of the argument.

Note that from the shell's perspective, the following are identical

foo "2014-05-01 01:00"
foo 2014-05-01\ 01:00

so if you are trying to force the output to look like the first one, don't: it's a cosmetic difference only.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • However, if there are blank fields (possibly generated by --colsep) then they don't get escaped. This is important when using -X to wrap each argument in quotes separately. I don't know of a work around beyond handling – removing – the backslash in the function you're calling, unfortunately. – davemyron Jan 07 '15 at 01:14
  • But, this answer doesn't help when the string is meant to be parsed by something other than the shell (like JSON or YAML). :-/ – Arel Dec 05 '18 at 00:14
  • JSON and YAML are *formats*, not parsers. If you need to generate JSON or YAML, you can. The point is, the OP was generating a literal backslash were one was neither needed nor wanted. – chepner Dec 05 '18 at 13:29
  • Yep, you're right (and fast to reply). I was confused by parallel's behavior when the command itself is in a literal string: `parallel './parse_yaml.py {key_1: true {1}}' ::: '' ', key_2: false' ::: # FAILS!` `parallel './parse_yaml.py "{key_1: true {1}}"' ::: '' ', key_2: false' ::: # FAILS!` `parallel './parse_yaml.py "{key_1: true "{1}"}"' ::: '' ', key_2: false' ::: # WORKS!` – Arel Dec 05 '18 at 17:10