sshrsynccircleci

Use Circle CI environment variable in string literal as part of a command parameter


I am new to Circle CI and all CI in general. I would like to call an rsync command with a custom port number using an envronment variable ($SSH_PORT) like so:

- run: rsync -ru -e 'ssh -p ${SSH_PORT}' ~/source/ $SSH_USER@$SSH_HOST:/dest

but my SSH_PORT variable is being taken literally instead of its value. I have tested the command with default port 22 and it works well.I suspect Circle CI uses bash to run these commands, but I'm not sure whether I'm handling a bash variable wrong or I'm not understanding Circle CI envronment variables well.

I have also tried:

- run: rsync -ru -e 'ssh -p "${SSH_PORT}"' ~/source/ $SSH_USER@$SSH_HOST:/dest
- run: rsync -ru -e 'ssh -p $SSH_PORT' ~/source/ $SSH_USER@$SSH_HOST:/dest

How do I get the varaible value instead of the variable name being taken literally?

For additional context, I tested this below and it works:

- run: rsync -ru -e 'ssh -p 1234' ~/source/ $SSH_USER@$SSH_HOST:/dest

Solution

  • A common bash/shell pitfall... Just replace your single-quotes (which prevent variable expansion) with double-quotes (https://phoenixnap.com/kb/bash-single-vs-double-quotes)

    - run: rsync -ru -e "ssh -p $SSH_PORT" ~/source/ "$SSH_USER"@"$SSH_HOST":/dest
    

    will work.

    I added double quotes in "$SSH_USER"@"$SSH_HOST" to comply with ShellCheck SC2086.