snyco
snyco

Reputation: 13

commands in indirect variables bash

My code below doesn't work. I am trying to simply save commands to a variable with indirect variable.

After executing I get:

*destL="/media/user/something" _destL=destL

rsync_home="rsync -avz --delete /home/ \$$_destL$home"

eval echo `$rsync_home`*

Output of this is :

*rsync: mkdir "/home/user/place where my script is located/$destL/home" failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(674) [Receiver=3.1.0]
sending incremental file list*

But when I tried to check what is in *$rsync_home* with command eval echo "$rsync_home"

Output is correct command:

rsync -avz --delete /home/ /media/michal/something/home/

Why this indirect variable is not shown in variable as it should, but as $destL ?

Upvotes: 1

Views: 92

Answers (1)

chepner
chepner

Reputation: 532043

Stop using eval, and define a function:

rsync_home () {
  rsync -avz -delete /home "$1"/home
}

Then call

rsync_home /media/user/something

Upvotes: 1

Related Questions