3

I have this shell script

#!/bin/sh
PATHS=( a b c d )

for PATH in ${PATHS[@]}
do
  rsync -avziP /home/user/$PATH $SERVER:$server_folder -b --backup-dir=$backup_folder/backup_$date --delete --exclude=.* --log-file=$HOME/rsync.log
done

And I always get this error:

rsync: command not found

What is driving me crazy is that if I delete the for loop, and just run the rsync command, the script works perfectly

José Luis
  • 3,713
  • 4
  • 33
  • 37
  • 3
    By the way, you're using arrays (`${PATHS[@]}`), which is bash-only feature. You're better off with specifying something like `#!/bin/bash` in your shebang, or it will probably blow sooner or later - most Linux distributions tend to remove bash as default sh implementation, BSD/Macs never had bash as sh. – GreyCat Nov 02 '12 at 09:53
  • 2
    Best practice is to use lower-case variable names (`path`) for anything that isn't either an environment variable or a builtin; this avoids stomping on reserved names (like `PATH`) by mistake. – Charles Duffy Mar 07 '15 at 00:44

1 Answers1

4

PATH is a reserved variable!

It is the variable specifying where to search tools (like rsync)

$ set | grep ^PATH=
PATH=/home/user/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

Use another variable name!

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137