0

Hello I have a problem in Bash scripting with executing command from other user, when trying to call there my own variables. It doesn't work: PATH='/home/test' sudo -u test -s /bin/bash -c 'rsync -az $PATH/ test@server1.example.com:$PATH'

It works only when I change the $PATH to /home/test which is not suitable. If will use another variable instead of PATH the result still the same. Problem that I do not know how to variables, when calling remote command. Is there any way to execute this command?

  • 2
    1) Don't you the variable name `PATH`, it has a special meaning. 2) In the `sudo` command, use double quotes (i.e. `"..."`) so that variables get expanded. – redneb Sep 07 '16 at 10:14
  • Why not just `sudo -u test rsync -az "$path" "test@server1.example.com:$path"`? You don't need another call to `bash` here. – chepner Sep 07 '16 at 12:58
  • I used another variable instead of PATH and nothing has been changed. – Ilya Kuznetsov Sep 07 '16 at 13:00
  • It also does not work with double quotes. – Ilya Kuznetsov Sep 07 '16 at 13:28
  • "Doesn't work" is not a good diagnostic. `path=/moo/foo/bar; sudo "echo $path"` has the shell interpolate `path` inside the `sudo` command before running it. `export path; sudo 'echo $path'` will work if `sudo` is allowed to import the exported variable. See the duplicate question for details. – tripleee Sep 08 '16 at 04:00

0 Answers0