Big Shield Big Shield - 5 months ago 21
Bash Question

How to sudo a bash script whose parent dir is in the $PATH?

For example

is in

cat ~/Desktop/scripts/hi

echo hi

What I have tried(The current dir is ~):

hi # CLI said "hi"
sudo -E hi # sudo: hi: command not found
se hi # sudo: hi: command not found # alias se="sudo -E "

How to sudo the script?

Answer Source

Try the following:

sudo PATH="${PATH}" bach -c "hi"

For the explanation please see man sudoers(5):

By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The new environment contains the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables in addition to variables from the invoking process permitted by the env_check and env_keep options. This is effectively a whitelist for environment variables.