I cannot find .bash_profile in ubuntu

Solution 1:

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

Solution 2:

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

Solution 3:

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.

Solution 4:

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.