Search code examples
bashrsync

script-file vs command-line: rsync and --exclude


I have a simple test bash script which looks like that:

#!/bin/bash

cmd="rsync -rv --exclude '*~' ./dir ./new"
$cmd # execute command

When I run the script it will copy also the files ending with a ~ even though I meant to exclude them. When I run the very same rsync command directly from the command line, it works! Does someone know why and how to make bash script work?

Btw, I know that I can also work with --exclude-from but I want to know how this works anyway.


Solution

  • Try eval:

    #!/bin/bash
    
    cmd="rsync -rv --exclude '*~' ./dir ./new"
    eval $cmd # execute command