How to create ~/.bash_profile and ~/.profile on Mac OSX

Mac OSX 15 มี.ค. 2015

You can use the touch command.

For instance,

cd ~

to go to the home directory.

Now we will create a file called .bash_profile (the dot means that it will be hidden).

Then use nano or vi in the Terminal. Unless you know what vi is, just use nano. To open up these files, you would use:

sudo nano .bash_profile

sudo makes sure that you will be able to save these files. Here, you can add aliases. nano or vi will automatically create a new file if it does not exist in your current directory in Terminal.

After you are finished, press Ctrl + O, Enter, and Ctrl + X to save and quit. Finally, use

source ~/.bash_profile

to reload the Terminal and it will read what you put in those files.

Of course, you can alias that too if you want. 🙂

To answer your final question, these files will automatically be read every time you open the Terminal. However, if there is an error (For instance, don’t put spaces between aliases), it will tell you.

More Information

As George pointed out, .bash_profile will run only on login shells. For non-login shells, you would need to create a .bashrc file with:

sudo nano .bashrc

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.