Hudson Marine
Hudson Marine

Reputation: 33

Using single quote (') in shell script - RSYNC woes

I think I have a simple problem I don't seem to be-able to find a solution to myself :-D

I am creating a shell script in linux (debian 10) using /bin/bash shell. Lines 1 & 2 below is the code. I am changing the default ssh port, however the single quotes I have put around 'ssh -p $PORT' for $SSH_CMD are translating into something weird, also see below for output lines 3 & 4. Any ideas how I can turn my single quotes into actual single quotes for the rsync line to work? Cheers.

SSH_CMD="-e 'ssh -p $PORT'"
rsync $SSH_CMD --chmod=a=rw,Da+x -trv $rsync_target/$target/manifest.csv $pendingdir/$target --delete

SSH_CMD='-e '\''ssh -p 46000'\'''
rsync -e ''\''ssh' -p '46000'\''' --chmod=a=rw,Da+x -trv [email protected]::download /ssd/rsync --delete

Upvotes: 0

Views: 249

Answers (1)

puravidaso
puravidaso

Reputation: 1223

After the variable expansion ($SSH_CMD), you want the shell to interpret the single quotes, and this is done via eval. Please see the following minimal example that works:

PORT=22
SSH_CMD="-e 'ssh -p $PORT'"
cmd="rsync $SSH_CMD /tmp/eval.sh test-w1:/tmp/"
echo $cmd
eval $cmd

Before you run it, you can echo out the command, and see if it is the command that you would type directly on the shell prompt.

Upvotes: 1

Related Questions