How do I set the $PATH environment variable?

I am very frustrated. All I want to do is to be able to run shell scripts, that are located on my desktop, in terminal. That's it. Of course, the $PATH environment variable has to be changed in order to do that. I've tried everything. I've tried editing etc/paths via the root account. I've tried creating a .bash_profile in my home folder. Neither works. I think at one point I got it to say "Permission Denied", and then I tried the same command with sudo behind it, I got a "command not found" error. Furthermore, when I type env in terminal to see what the current path is, the last path(there are multiple paths in the PATH variable) is nowhere to be found in the etc/paths file. The rest of the PATH variable is in etc/paths, but the last one isn't. When I last edited etc/paths by just pasting the appropriate path on a new last line in the file, when I typed env in terminal, it add stuff that I didn't type. Like I pasted Users/Me/Desktop in etc/paths, and when I typed env, that part part of PATH said Users/Me/Desktop/sbin:sbin. Oh, and if it's of any help, if I just type $PATH in terminal, I get a 'no such directory' error.

I think I'm missing something. This shouldn't be this hard. I'm very frustrated over something that should be simple. Any assistance is appreciated.

Mac OS X (10.6.2)

Posted on Feb 19, 2010 8:00 PM

Reply
6 replies

Feb 20, 2010 9:18 AM in response to Jon R

Tony T1 has the PATH setup information. You will have to exit from the Terminal session and start a new one before you will see the PATH change.

You know you can always run the script by typing

~/Desktop/name-of-your-script

Then again, not having to enter the full path is much nicer, so getting PATH correctly setup is nice.
... I think at one point I got it to say "Permission Denied",

That generally means your script does not have execute permission.

chmod +x ~/Desktop/name-of-your-script

and then I tried the same command with sudo behind it, I got a "command not found" error.

Without seeing the exact error message, which is might look something like:

sudo: abc: command not found

it is not always easy to diagnose the problem. The full message tell us which component could not find the command.

Anyway, if it was like the above, then it could be one of 2 things. The directory was not in PATH or the command mistyped (a typo).

Feb 20, 2010 10:04 AM in response to Jon R

Jon R wrote:
All I want to do is to be able to run shell scripts, that are located on my desktop, in terminal.
Of course, the $PATH environment variable has to be changed in order to do that.


Not necessarily. If you want to run scripts in some non-standard location, the normal procedure is to just refer to them with a relative path. Run your script with "./myscript.sh".

I've tried editing etc/paths via the root account.


Don't do that. If you want to add system-wide paths, use path_helper. I strongly encourage people to use this feature. It is very handy. All you do is create a file in /etc/paths.d that contains a single line, the directory you want to add to your path. The file "X11" is already there. Just copy that file, rename it, and change the contents to be your own path. Of course, path_helper is shared by all users of your machine, so it might not be appropriate for scripts on your desktop.

I've tried creating a .bash_profile in my home folder. Neither works. I think at one point I got it to say "Permission Denied", and then I tried the same command with sudo behind it, I got a "command not found" error. Furthermore, when I type env in terminal to see what the current path is, the last path(there are multiple paths in the PATH variable) is nowhere to be found in the etc/paths file. The rest of the PATH variable is in etc/paths, but the last one isn't.


It sounds like you are doing .bash_profile correctly then. It is just that the path is not correct.

When I last edited etc/paths by just pasting the appropriate path on a new last line in the file, when I typed env in terminal, it add stuff that I didn't type. Like I pasted Users/Me/Desktop in etc/paths, and when I typed env, that part part of PATH said Users/Me/Desktop/sbin:sbin. Oh, and if it's of any help, if I just type $PATH in terminal, I get a 'no such directory' error.


Don't edit /etc/paths. Use path_helper if you want a system-wide path. If you want a path in your home directory, use .bash_profile. I think all you are missing the root path. Your path setting should look something like this:

# My custom path.
export PATH="~/Desktop/sbin:$PATH"

Feb 20, 2010 2:07 PM in response to Tony T1

Add to .bash_profile:

PATH=$PATH:~/Desktop
export PATH

To see the path, type echo $PATH

Thinking about this a little bit, it might be safer to use $HOME instead of ~ and to put the assignment in double quotes to protect against spaces which might exist in some of the directory paths

export PATH="$PATH:$HOME/Desktop"

And if the scripts are really in a subdirectory sbin, then

export PATH="$PATH:$HOME/Desktop/sbin"

Feb 25, 2010 5:03 AM in response to Jon R

The customary UNIX way of executing own scripts is to create a 'bin' subdirectory in your home directory (i.e. $HOME/bin). Having those scripts on Desktop works quite as well, but...
The problem in modifying shell startup scripts is that the settings are not immediately in effect. You can log out/log in, create a new Terminal.app window, or use the 'source' command ('source ~/.bash_profile').
Another thing is that if you want to set variables for GUI programs, you have to modify the environment.plist file found in ~/.MacOSX-directory (or create it, if it does not exist)...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I set the $PATH environment variable?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.