1

I use the following command, as part of a script, to backup from my W10 machine with WSL to a Debian box:

rsync -PaSh --stats --delete -e 'ssh -p XXXX -i ~/keys/key' /mnt/c/Users/User/Desktop/ user@hostname.com:/home/user/ 2>> /mnt/c/Users/User/Documents/output.txt | grep -e 'Number of regular files' -e 'Total transferred' -e 'bytes/sec' >> /mnt/c/Users/User/Documents/output.txt

This works fine and correctly pipes output.

To simplify, I'd like to replace the grep pattern with a variable:

arguments="-e 'Number of regular files' -e 'Total transferred' -e 'bytes/sec'"

rsync -PaSh --stats --delete -e 'ssh -p XXXX -i ~/keys/key' /mnt/c/Users/User/Desktop/ user@hostname.com:/home/user/ 2>> /mnt/c/Users/User/Documents/output.txt | grep $arguments >> /mnt/c/Users/User/Documents/output.txt

This gives me:

grep: of: No such file or directory
grep: regular: No such file or directory
grep: files': No such file or directory
grep: transferred': No such file or directory

How can I tell grep to correctly recognise the spaces?

NoExpert
  • 489
  • 1
  • the shell isn't a macro processor: quotes and other syntax elements that you've stored in a variable don't get used as shell syntax just by expanding the variable any more than they would in other programming language. Put the arguments in an array as individual strings and use that array. args=(-e 'Number of regular files' -e 'Total transferred' ...) etc. – ilkkachu Aug 08 '21 at 19:42
  • Thanks. The solution is the pseudo-array "$@" described here: https://unix.stackexchange.com/questions/444946/how-can-we-run-a-command-stored-in-a-variable. Thanks for your replies. – NoExpert Aug 08 '21 at 19:58

0 Answers0