1

On LMDE-based laptop I have a script upgrade.sh which only runs the following commands:

#!/bin/sh

apt-get upgrade
apt-get dist-upgrade

The script is in the /home/francois/scripts directory. I can easily run it from within this directory. It just works. Now I run the following commands:

$ sudo ln -s /home/francois/scripts /opt/scripts
$ sudo ln -s /opt/scripts /usr/local/bin/scripts

and create file /etc/profile.d/myscripts.sh

with just one line:

export PATH=$PATH:/usr/local/bin/scripts

I type

$ echo $PATH

and see that it contains /usr/local/bin/scripts

But when I run

$ sudo upgrade.sh

from any directory the system says that the command not found

ls -l shows -rwxr-xr-x 1 francois francois ...

jww
  • 97,681
  • 90
  • 411
  • 885
  • Is your script actually marked executable? And you need a shebang for it to be executed by anything other than a shell. [Edit] the question to show file permissions (ie. output of `ls -l upgrade.sh`). – Charles Duffy May 03 '17 at 14:38
  • ...which is to say, this is fairly likely to be a duplicate of [How do I run a shell script without using `sh` or `bash` commands?](http://stackoverflow.com/questions/8779951/how-do-i-run-a-shell-script-without-using-sh-or-bash-commands), if it isn't duplicative of a `sudo`-specific question. – Charles Duffy May 03 '17 at 14:38
  • 1
    Possible duplicate of [sudo changes PATH - why?](http://stackoverflow.com/questions/257616/sudo-changes-path-why) – Eric Renouf May 03 '17 at 14:39
  • ...note that a *shell* will let you get away without a shebang line (`#!/bin/bash` or similar), but when launching software from any non-shell program (and `sudo` is not a shell!), it's strictly required. – Charles Duffy May 03 '17 at 14:40
  • @CharlesDuffy You can definitely run a script that doesn't have a shebang with sudo. – 123 May 03 '17 at 14:51

1 Answers1

1

Sudo doesn't inherit the caller's environment variables. $PATH is one of those variables.

To make it inherit the environment variables of the shell it's invoked from, run it as sudo -E [cmd]

amphetamachine
  • 27,620
  • 12
  • 60
  • 72