watch command not working with special characters and quotes

795 views Asked by At
watch -n 1 "paste <(ssh ai02 'nvidia-smi pmon -s um -c 1') <(ssh ai03 'nvidia-smi pmon -s um -c 1' )"

The above command is used to horizontally stack two server GPU stats together. It works without the watch command but get the following error

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `paste <(ssh ai02 'nvidia-smi pmon -s um -c 1') <(ssh ai03 'nvidia-smi pmon -s um -c 1' )'
1

There are 1 answers

0
Ronaldo Ferreira de Lima On BEST ANSWER

You didn't provide a reproducible example, but I think I managed to make one for testing:

watch -n1 "paste <(seq -w 1000 | shuf -n '10' ) <(seq -w 1000 | shuf -n '10')"

output a similar error:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `paste <(seq -w 1000 | shuf -n '10' ) <(seq -w 1000 | shuf -n '1
0')'

To solve this problem in a simpler way, we can change sh -c for bash -c:

watch -n1 -x bash -c 'paste <(seq -w 1000 | shuf -n "10" ) <(seq -w 1000 | shuf -n "10")'

From the watch manual:

-x, --exec
     Pass command  to exec(2) instead of  sh -c which reduces  the need to
     use extra quoting to get the desired effect.

If you need maintain the apostrophes from the original commandline, you can escape then too:

watch -e -n1 -x bash -c 'paste <(seq -w 1000 | shuf -n '\''10'\'' ) <(seq -w 1000 | shuf -n '\''10'\'')'