31

So, I don't have .bash_profile neither .profile in my home folder. How do I create them? After that, what should I do so every time I open the terminal these files get read?

4 Answers 4

33

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

Links

.bash_profile vs .bashrc

Importance of .bashrc

5
  • 6
    Why are you using touch to create empty files first? nano does this automatically anyway.
    – nohillside
    Commented Aug 24, 2013 at 14:23
  • 5
    Also, you probably don't want both ~/.profile and ~/.bash_profile -- if bash sees .bash_profile, it doesn't bother with .profile. Finally, it'll only get run for "login" shells; nonlogin shells run ~/.bashrc instead. See previous SO questions here and here. Commented Aug 24, 2013 at 16:06
  • @GordonDavisson is it good to have ~/.profile as a copy of ~/.bash_profile? And if you have both, what do you have inside ~/.profile that can't be done in ~/.bash_profile?
    – Thi G.
    Commented Aug 25, 2013 at 17:55
  • 2
    @ThiG: I wouldn't have one be a copy of the other; you're likely to add something to one, forget to add it to the other, and get confused. See my recommendations on this SO question for what I'd consider best practice. Commented Aug 25, 2013 at 20:16
  • sudo makes sure you mess up the user rights. Don't do that.
    – bot47
    Commented Nov 6, 2019 at 21:06
3

You can also use a GUI text editor like TextEdit:

touch ~/.bash_profile
open -e ~/.bash_profile

open -e is a shortcut for open -a TextEdit.

You don't necessarily have to create .profile or .bashrc. Terminal and iTerm 2 open new shells as login shells, so bash doesn't read .bashrc. If both .bash_profile and .profile exist, bash reads only .bash_profile when it is invoked as an interactive login shell. .profile is read by ksh when it is invoked as an interactive login shell and by bash when it is invoked as sh as an interactive login shell.

I have actually told iTerm 2 to open new shells as non-login shells, and my .bash_profile just contains a line like . ~/.bashrc. tmux and the shell mode in emacs open new shells as non-login shells by default. .bash_profile is still read when I ssh to my computer.

2
  • Let me see if I got that... You have both ~/.bash_profile and ~/.profile? And you also have ~/.bashrc?
    – Thi G.
    Commented Aug 25, 2013 at 18:00
  • I don't have a .profile, and my .bash_profile just contains . ~/.bashrc.
    – Lri
    Commented Aug 26, 2013 at 7:28
3

You can copy those files from /etc/skel/ which are skeleton files for new users created by command like useradd on Debian based distros:

cp -nr /etc/skel/. ~/

These files will be automatically loaded by shell every time you log in. Way of loading those profile files is described in your shell manual page. In case you use bash shell in special way (e.g., via ssh, ansible, etc...) you should use 'bash -ilc command' if you want execute command in bash shell with ENV prepared by those profile files

  • -i means interactive shell what is often required by .bashrc
  • -l means login shell which causes .profile to be loaded (or .bash_profile if exists - see manual page of bash) which then loads .bashrc (if interactive)
1
  • /etc/skel/ doesn't exist on macOS. macOS isn't even Linux, let alone Debian-based. Commented Feb 9, 2021 at 1:44
0

I prefer to use GUI text editors such as BBEdit or TextWrangler. These text editors can handle invisible files such as .profile and .bash_profile and they have the advantage of having a GUI interface, unlike nano which is a console interface.

TextWrangler

BBEdit

0

You must log in to answer this question.

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