106

I can't find .bash_profile in Ubuntu 14.04 in my /home/user directory. I used the ls -a command to see the .bash_profile, but there isn't such a file.

Zanna
  • 70,465
Roledenez
  • 1,185
  • 2
  • 10
  • 9

5 Answers5

137

Ubuntu uses ~/.profile .

you can create your .bash_profile in Ubuntu but then .profile will not be read.

If we read .profile content :

cat ~/.profile

output

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.

So use ~/.profile instead of ~/.bash_profile

nux
  • 38,017
  • 35
  • 118
  • 131
16

When invoking a login shell bash will looks for its config files in this order:

[0] ~/.bash_profile
[1] ~/.bash_login
[2] ~/.profile

After finding the first one, it stops looking for the others so if there is a .bash_profile in my $HOME bash will not look for .bash_login and .profile anymore.

From these three file names, Ubuntu by default uses .profile you can rename it to .bash_profile if you like:

mv ~/.profile ~/.bash_profile

Now if we open a new bash shell using bash -l, su - $USER, sudo -u $USER -i or any other commands that runs bash as a login shell, ~/.bash_profile will get sourced.

Important to note:

What I have talked about till now only applies to Bash itself, when you are logging into the system from a GUI, the display manager is responsible of sourcing the correct files.

Ubuntu uses gdm3 as its display manager, if we take a look at: /etc/gdm3/Xsession we can see that none of the files will get sourced except: .profile:

# First read /etc/profile and .profile
for file in /etc/profile "$HOME/.profile"; do
  if [ -f "$file" ]; then
    source_with_error_check "$file"
  fi
done

so if you are using a GUI to login, keep the file under .profile name otherwise you might miss some variables and settings in your environments.

I guess the better option is creating a symlink to .profile:

ln -s ~/.profile ~/.bash_profile

Now your data lives in .profile, gdm doesn't miss anything, bash loads .bash_profile which is actually .profile, and by editing each of them you get the same result.

Missing .profile?

If you don't have .profile then grab a copy of it from here:

cp /etc/skel/.profile ~/.profile

or

# Remember the note above
cp /etc/skel/.profile ~/.bash_profile
Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • 1
    But, if Bash is looking for ~/.bash_profile first, then how come it isn't doing this in Ubuntu? Has Ubuntu modified Bash? This seems strange. man bash on Ubuntu still describes how it will look for .bash_profile first. – Jonathan Hartley Sep 22 '18 at 01:50
  • It looks for .bash_profile first it can't find it (because it does not exists) then looks for .bash_login can't find it again (because Ubuntu does not uses these two name) finaly it looks for .profile and yes there it is. So if you create a .bash_profile in ~ then you are overwriting ubuntu's ~/.profile, it's the default behavior of bash nothing has been modified here. – Ravexina Sep 22 '18 at 07:59
  • Right, that's what I thought at first, too! And what you describe DOES happen on RHEL. But on Ubuntu, it does not. If I create a .bash_profile, it is not read. I just realized that this is because Ubuntu no longer uses 'Bash' as a GUI login shell. It uses 'Dash' instead. I think this has been true since about 18.04. – Jonathan Hartley Sep 22 '18 at 13:59
  • You are missing the point here. Actually we are talking about the bash itself, what I'm saying here is that Ubuntu creats ’.profile’ and not the others, so if we open a new bash shell (login shell) that's the order of reading these files, doesn't matter what distro we are using. :) – Ravexina Sep 22 '18 at 14:54
  • If readers rename .profile to .bash_profile as your answer suggests, then for people on Ubuntu, that file will not be read on login, nor on opening a terminal. You have broken their system. The only way that file will get read is if they launch bash sessions with the '--login' arg to explicitly make it a login shell. That will never happen automatically on their desktop. – Jonathan Hartley Sep 22 '18 at 18:32
  • @JonathanHartley You are both right and wrong, you are right about .bash_profile not being sourced while logging in from GUI but wrong about dash being used as login-shell, read here also removing .profile wouldn't broke the system... I'll investigate a little bit more and then I'll update my answer. thanks for pointing the issue out :) – Ravexina Sep 23 '18 at 14:20
  • @JonathanHartley Updated the answer ... – Ravexina Sep 23 '18 at 16:55
  • Good update. Nice work. I see what you say about Dash not being the login shell. I read otherwise elsewhere, and if Bash is the login shell then I don't understand why it doesn't source .bash_profile on login. But presumably I'm just confused about that somehow. – Jonathan Hartley Sep 23 '18 at 17:47
  • @JonathanHartley As I said, while using GUI, gdm is responsible of sourcing the config files and make environment ready, however you can press CTRL+ALT+F2 or any other "Fx" to go into a tty, then login using your credentials, now based on what is your default login shell (getent passwd $USER | cut -f7 -d:) that shell will be used as a login and interactive shell, if it's bash then .bash_profile will get sourced. – Ravexina Sep 23 '18 at 18:04
  • Yep yep, I realize all that. – Jonathan Hartley Sep 23 '18 at 22:38
5

That means the file does not exist. But, you can create the file and bash executes/sources the file if bash is invoked as a login shell. So evertime you login via a shell (for example via ssh).

If you want the content to execute everytime you open a terminal, then you should modify the .bashrc file instead.

chaos
  • 27,506
  • 12
  • 74
  • 77
3

Top answer to use ~/.profile instead of ~/.bash_profile did not work for me.

Modifying .bashrc worked

Just:

vim ~/.bashrc

Note: I'm using Ubuntu WSL.

cryanbhu
  • 150
  • In genuine Linux, the .profile is applied when you login to the GUI, or connect remotely, or log in to the console, ie after pressing ctrl+shift+F2. It's supposed to represent the initial time you login to the machine, creating a shell which is parent to all other processes you subsequently create. I suspect that on WSL, there is no such thing as any of this, so your .profile might never be read. – Jonathan Hartley Sep 22 '18 at 19:53
  • same for me. ~/.profile just not loaded by default in ubuntu 18.04 – Oleg Vazhnev Jun 13 '19 at 13:41
0

If you mean the .bashrc you will find it in your home folder. If it isn't there, you can copy it from the /etc/skel folder to your home folder.

If you need some more information on this subject, please visit stefaan lippens page.

http://stefaanlippens.net/bashrc_and_others

Lie
  • 1