Search code examples
bashrsync

Rsync command variable, bash script


RSYNC="rsync -avzhe 'ssh -i /path/to/deploy_keys/id_rsa' --delete "

# Files
$RSYNC deploy@ip:/var/www/path1 /var/www/path1
$RSYNC deploy@ip:/var/www/path2 /var/www/path2

I'd like to introduce this RSYNC Variable to be more compact, but it throws an error:

Unexpected remote arg: deploy@ip:/var/www/path1

If i use only rsync inside the doublequotes, it works fine. For the sake of readability, i'd keep them separate command invocations.


Solution

  • I agree that eval is dangerous. In addition to the array approach @Eugeniu Rosca suggested, you could also use a shell function:

    my_rsync() {
        rsync -avzhe 'ssh -i /path/to/deploy_keys/id_rsa' --delete "$@"
    }
    
    my_rsync deploy@ip:/var/www/path1 /var/www/path1
    my_rsync deploy@ip:/var/www/path2 /var/www/path2
    

    BTW, you should read BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!.