How do you save an environmental variable?

Solution 1:

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

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