Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've modified my .profile to include a folder if a flash drive is plugged in. When running the command as the user it works fine but tells me the scrip must be run by sudo (this is what i want). However, when i try to run it with sudo i get "command not found"

I have a symlink (flash) in my /var/www folder pointing to my /media/flash drive. (nevermind this setup since is just for dev)

this is my user's .profile :

# set PATH so it includes flash scripts
if [ -d "/var/www/flash/scripts" ] ; then

when trying to run as sudo i get:

sudo: script: command not found    

any ideas?

share|improve this question

When you run the script as sudo you are trying to access root. So your $HOME/.profile wont be accessed instead /root/.profile will be accessed.

So make the changes what ever you did in $HOME/.profile to /root/.profile. To create /root/.profile. Use the following commands.

sudo su
cd /root
touch .profile

after making the changes try running the script.

or edit $HOME/.bashrc and add the following line

alias sudo='sudo env PATH=$PATH $@'

Hope this helps.

share|improve this answer
I tried "alias sudo='sudo env PATH=$PATH $@'" but i get "env: wp: Permission denied" – user564448 Nov 9 '12 at 17:35
sorry, "wp" is the script – user564448 Nov 9 '12 at 17:47
Try the other step I mentioned. – devav2 Nov 9 '12 at 17:49
Read it here you should use sudo su -p – devav2 Nov 9 '12 at 17:53

that is the behavior of sudo in ubuntu..

for example run following command

echo 'echo $PATH' | sh

and than this one

echo 'echo $PATH' | sudo sh 

you see the output is different! to avoid different paths put the following in your ~/.bashrc

alias sudo='sudo env PATH=$PATH'
share|improve this answer
+1. You are right. – devav2 Nov 9 '12 at 17:29
After putting alias sudo='sudo env PATH=$PATH' into my .bashrc i get "env: wp: Permission denied" – user564448 Nov 9 '12 at 17:33

You can preserve the value of $PATH when using sudo. Use visudo to add $PATH to the list of variables to keep. A line like the following in /etc/sudoers should do the trick:

Defaults env_keep += "PATH"
share|improve this answer

You can also move your file in a sudoers used directory :

    sudo mv $HOME/bash/ /usr/sbin/ 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.