1

I have the following script in my user's bin directory; let's pretend the filename is 'myScript':

#!/bin/bash

python3 /myPath/myProgram.py "$@"

From the commandline, if I type myScript, it launches my myProgram.py. If I type sudo myScript, however it gives me this error: sudo: myProgram: command not found.

Why doesn't this work? How can I make it work?

Thanks!

  • Have you tested sudo /path/to/myScript – A.B. Aug 21 '15 at 11:02
  • @A.B. Sorry, I had a mistake in my question, which I edited out. It works if I'm in the bin directory and type sudo ./myScript. So, yes, with the full path it works. However, putting things in the bin directory makes it so you can run them without typing the path. So, I guess getting it to work with that functionality is what I'm asking about. – Brōtsyorfuzthrāx Aug 21 '15 at 11:04
  • Add your local bin folder to the PATH environment. – A.B. Aug 21 '15 at 11:06
  • 2
  • 1
    Edit your ~/.bashrc – A.B. Aug 21 '15 at 11:08
4

You probably need to run the script like:

  • sudo /path/to/myScript
  • sudo ./myScript (if the script is in the current directory)

The reason you get command not found is the script is not in the current user's PATH directory. However, using sudo runs it as a different user - root (compare echo $PATH and sudo -i then echo $PATH to see). So of you want to add it to the root user's PATH:

  • Copy it to /usr/local/bin, /usr/bin/ or another direcotry already in the root's PATH
  • Add the directory to the PATH, but do it as root (carefully).
| improve this answer | |
  • This is a very good explanation, well done. – fedorqui Aug 21 '15 at 11:15
  • That makes perfect sense, but adding the script to /usr/local/bin and restarting didn't solve the problem (although it's in both my path and the root path). However, I decided to resort to update-alternatives (I already knew about that, but I thought there might be an easier way to do it without it: update-alternatives --install /usr/bin/myScript myScript /home/myUsername/bin/myScript 0 – Brōtsyorfuzthrāx Aug 21 '15 at 11:40

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