Question

I'm having a problem with bash's quoting rules and I don't know how to solve it. I want to have a variable inside some kind of config file, where a list of commandline options can be specified. In my case it's for rsync: rsync has the commandline-option '--rsh', which allows you to specify the remoteshell it's using on the other side. For example:

rsync -v --rsh="ssh -p4711 -lhttp" /tmp/testfile remoteMachine.x:/tmp/testfile

This works perfectly! But if you try to do this inside a script, where you want to allow all options to be specified in a variable, like this:

#!/bin/bash

OPTS="-v --rsh=\"ssh -p4711 -lhttp\""
rsync $OPTS testfile remoteMachine.x:/tmp/testfile

When exexuting this scrip, it will fail with this error:

rsync: -p4711: unknown option

This is, because bash is doing some nasty quoting/escaping and I can't figure out why (You can see what bash's doing with 'bash -x'):

+ OPTS='-v --rsh="ssh -p4711 -lhttp"'
+ rsync -v '--rsh="ssh' -p4711 '-lhttp"' testfile remoteMachine.x:/tmp/testfile

I tried multiple combinations of weak and strong quotes(", ') but nothing works... Can you help me, why bash is doing that and how I can solve this strange behavior?

Thanks and greetings from Cologne!

Was it helpful?

Solution

Use an array:

OPTS=(-v --rsh="ssh -p4711 -lhttp")
rsync "${OPTS[@]}" testfile remoteMachine.x:/tmp/testfile

OTHER TIPS

Two alternatives: pass the --rsh options through RSYNC_RSH environment variable or pass the port and login options to rsync directly with --port and the login@host:path syntax

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top