I want to execute a command in a bash script which has this style:

rdiff-backup --verbosity 5 -somearguments -somemorearguments \
    --remote-schema 'ssh -i $keyfile -C %s' \
    sourcedir backup::/targetdir

The problem appears with the --remote-schema argument. I have to make the ssh-key variable, in this example with $keyfile.

I construct the resulting command in $rdiffcmd. I do not have a problem to construct a command where everything looks like it should work: An echo $rdiffcmd shows the necessary quotes around --remote-schema. But there has to be an invisible expansion because I always get the message that rdiff-backup (on the local side) does not understand -i. So it is not using the quotes.

What do I have to do to group the remote-schema together and make it look like one argument?

Very strange: I can paste and copy the output of echo $rdiffcmd in a terminal and it works. But as $rdiffcmd in my script it does not with the above problem.

  • 3
    Use double quotes. " vs '. Single quotes are literals, variables within are not expanded. Here's how bash handles the various expansions: gnu.org/software/bash/manual/html_node/Shell-Expansions.html Mar 17 '16 at 9:57
  • Oh, that was easy. Thanks. By the way: Is there a possibility to group an argument inside the argument? I now need to give a directory-name also. But directory names can contain spaces. As remote-schema is already encapsulated with " the additional " around the directory name seems not to have the wanted result. Mar 17 '16 at 11:16
  • Escape characters with backslashes. e.g. " \"quoted within quotes\" ". The first expansion will happen on the local side, removing the outer quotes then the escaped quotes will be removed on the remote side. Mar 17 '16 at 12:48
  • @css1971 That doesn't work if the ‘quoted within quotes’ bit can itself contain quotes. user2715068 : it's tricky. Edit your question to add some sample code that works without special characters so that we know exactly what you're trying to do. Mar 17 '16 at 23:04

If $keyfile contains spaces and whatnot:

rdiff-backup --verbosity 5 -somearguments -somemorearguments \
    --remote-schema 'ssh -i "'"$keyfile"'" -C %s' \
    sourcedir backup::/targetdir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.