Skip to content

Editing .bashrc and .bash_profile

Editing .bashrc

To edit the .bashrc file, I ran the following command to open it in Vim

console
vim ~/.bashrc

The ~/ is a shortcut to access the user's home directory.

Using Vim to edit the .bashrc file, I added a line that echoes a simple message.

Editing .bashrc

This way when I create a new Bash session, I will see that text echoed since the new session will be a non-login shell.

Testing .bashrc

Clearly the edits worked as the image above shows.

Editing .bash_profile

To edit the .bash_profile file, I once again opened it up using Vim

console
vim ~/.bashrc

Similarly to editing .bashrc, I added a line that echoes a simple message.

Editing .bash_profile

To test it, I re-entered the Ubuntu instance by running wsl -d Ubuntu. I did this because as the previous page mentioned, this would create a login shell.

Testing .bash_profile

Clearly the edits worked as the image above shows.

Common Practices

A common practice with .bash_profile is to source .bashrc. To source a file means to execute commands from that file from within the current shell.

To do this, I edited the .bash_profile by adding the following code to it

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

Adding this code means that the "This is .bashrc" text should show up when using a login shell.

To test this, I once again re-entered the Ubuntu instance by running wsl -d Ubuntu

Testing the common practice

Clearly the .bashrc file is being executed, as it should since it was sourced by .bash_profile.

The purpose of sourcing .bashrc within .bash_profile is so that non-login shells and login shells have consistent settings.