Export PATH directly from command line in Yosemite--where is my PATH?
Solution 1:
On June 3 you posted the following comment
@DavidAnderson So if I understand correctly, running export PATH... from an interactive shell session only affects $PATH for that session and the newly added path is not saved anywhere (and therefore there is no file where the newly added path can be edited). E.g., if I run export PATH=/test:$PATH but I meant to export the /testing_123 dir instead, then my best option is to simply start a new session rather than attempt to locate and edit a file containing the erroneous /test dir (because there is no such file). Correct?
My response to your comment
From the manual page for bash, I have extracted the following.
A parameter is an entity that stores values. It can be a name, a
number, or one of the special characters listed below under Special
Parameters. A variable is a parameter denoted by a name. A variable
has a value and zero or more attributes.
PATH
is the name of a variable. Normally, this variable has only the export attribute turned on. The export attribute marks a variable for export to subsequent commands via the environment. The export
command turns on the export attribute, but in the case of the PATH
variable, the export attribute is already turned on. Therefore, for the PATH
variable, the next four commands are equivalent.
PATH=/test:$PATH
export PATH=/test:$PATH
declare PATH=/test:$PATH
typeset PATH=/test:$PATH
As for your comment, you are correct. There is no such file.
In the case of a mistake of the type you mentioned, I do not think starting a new session would be the best option. I would enter the command
declare -p PATH
and then highlight the output. You can then cut and paste the text as your next command. Before hitting the return key, use the other keys to correct your mistake.
For more information on this subject, see the manual page for bash. Either enter the command
man bash
or visit the site: bash - GNU Bourne-Again SHell.
In response to your Edit:
Yes, you are suppose to lose /test in your PATH variable when you reboot. This is why you need to put an export
command in your ~./profile
file.
Original answer:
You seem confused when the ~/.bashrc
, ~/.bash_profile
, and ~/.profile
files execute. Maybe this will help.
Here is how I set up my computer. I have three files: ~/.profile
, ~/.bashrc
and ~/.bashcm
. If you are missing any of these, you can create them.
When I start the Terminal application or select "New Window" from "Shell" on the menu bar, an interactive bash login shell is invoked. This shell executes the ~/.profile
script at startup. You can substitute ~/.bash_profile
or ~/.bash_login
for ~./profile
if you wish. If an interactive bash shell that is not a login shell is started, the shell reads and executes commands from ~/.bashrc
. The purpose of ~/.bashcm
is to provide a common script to be executed by both ~/.profile
and ~/.bashrc
.
Here is an example of a .profile
file
echo in .profile
source ~/.bashcm
export PATH=$PATH:~/bin
echo exit .profile
The purpose of the echo commands are to illustrate when the scripts are executed. Once you are comfortable, you can either comment out or delete them. The .profile
file executes .bashcm
script, then modifies the PATH variable and exits. An example .bashcm
file is shown below.
echo in bashcm
PS1='\h:\W \u\$ '
alias home='cd ~'
echo exit .bashcm
Other than the echo commands, .bashcm
only sets the prompt and an alias. The example .bashrc
file shown below simply executes the .bashcm
script. Again the echo commands are just for illustration purposes.
echo in .bashrc
source ~/.bashcm
echo exit .bashrc
If I start the Terminal application and enter the commands
echo $PATH
alias
bash
echo $PATH
alias
in the window, I get the following.
Last login: Wed May 27 14:48:19 on ttys000
in .profile
in bashcm
exit .bashcm
exit .profile
Steelhead:~ davidanderson$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/davidanderson/bin
Steelhead:~ davidanderson$ alias
alias home='cd ~'
Steelhead:~ davidanderson$ bash
in .bashrc
in bashcm
exit .bashcm
exit .bashrc
Steelhead:~ davidanderson$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/davidanderson/bin
Steelhead:~ davidanderson$ alias
alias home='cd ~'
Steelhead:~ davidanderson$