How to save an environmental variable

bashcommand line

Trying to save an environmental variable.

cd ~
mkdir Projects
export PROJDIR=/Users/David/Projects
cd ~
cd $PROJDIR //can execute.
cd ..
open .bash_profile //was told this was the file you add the variable to.

Then terminal tells me there is nothing with that name.
To check

ls -a

And there are two files with bash .bash_history and .bashsessions
.bash_history isn’t executable and when I open .bashsessions I’m not sure how I’d add

export PROJDIR=/Users/$USER/Projects

to the file. So that doesn't seem right either.

Best Answer

‘export’ doesn't write anything to file. It's for making variables available to subprocesses.

If you've never written anything to .bash_profile, it's unsurprising that it doesn't exist, since it's not created for you. You'll need to create it yourself and write that line within.

For example, touch ~/.bash_profile (to create the file) then edit it in an editor of your choice such as TextEdit (open using Finder or open -e ~/.bash_profile). Alternatively, nano ~/.bash_profile. Paste in export PROJDIR=/Users/$USER/Projects and save the file, then reopen your shell or source the profile.