Big Shield
Big Shield

Reputation: 622

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

For example

~/Desktop/scripts is in $PATH

cat ~/Desktop/scripts/hi

#!/bin/bash
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?

Upvotes: 0

Views: 115

Answers (1)

Andriy Berestovskyy
Andriy Berestovskyy

Reputation: 8544

Try the following:

sudo PATH="${PATH}" bash -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.

Upvotes: 1

Related Questions