I wrote a utility script to do some automatic moving and copying stuff, put it in a directory, exported the dir in $PATH with bashrc and it worked well with my normal user. But when I tried to sudo the script, it couldn't be found. Copy bashrc for root didn't work better. Finally google helped me out: it's the secure_path stuff on my way.

In fact, when we sudo a command, the system makes a env_reset first. So all our settings in bashrc are lost, including customized $PATH. So the solution is to modify /etc/sudoers, either we completely disable the secure_path reset:


Defaults !secure_path

the line is to be added right after the first:

Defaults env_reset

or we add our useful path to secure_path, like what we do with $PATH in bashrc:

Defaults secure_path="/the/useful/path"