I need to escape single quotes in a variable.
ssh_command 'file=$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf $file ini | wc -l | xargs printf \'Num files: %d, File: $file\'' I can surround the variable with double quotes, but then the internal variables will be evaluated when the variable is declared, and that's not what I want.
ssh_command "file=$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf $file ini | wc -l | xargs printf 'Num files: %d, File: $file'" Update
I have now come up with this, but then $file is just printed as $file
ssh_command (){ ssh root@host $1 } ssh_command 'file=$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf $file ini | wc -l | xargs printf '"'"'Num files: %d, File: $file'"'" Output
Num files: 61, File: $file
$fileis declared in the command line in the beginning.. Output:Num files: x, File: /path/to/tar'"'"'. For the reason why, see the duplicate post comment.'"'"'but now$fileis not set in the last part