5

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.

3

‘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.

1
  • You can write it to .bashrc as well. – m4p85r Mar 21 '17 at 0:43
2

Redirection is one of the simplest ways to create a file with text or append a file with text. Standard output is redirected with the > symbol or appended to a file with >>.

In your case, you want the string export PROJDIR=/Users/$USER/Projects added to .bash_profile. We will protect the string with single (strong) quotes, so $USER is not expanded.

cd  # no need for the tilde (~)
echo 'export PROJDIR=/Users/$USER/Projects' >>.bash_profile

A single > will overwrite any text in a file, so use >> and append the text for safety. It would benefit you to learn one of the three visual editors (vi/vim, emacs, nano) supplied in macOS.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .