Home » Linux » Shell » Bash Profile

The Bash Profile and How to Use It

If you’re frequently interacting with Linux via the Bash shell, you’ll eventually want to customize it a bit – perhaps adding your own shortcuts, or setting up the environment to your liking, or even just adding some decorative personalization.

This is what the Bash profile is for. It’s stored in your home directory and can be edited to set things up just the way you want each time you log in.

Editing your Bash Profile

To edit your bash profile, open it with the nano text editor by running:

nano ~/.bash_profile

If you want to back up your current profile, make a copy of it by running:

cp .bash_profile .bash_profile.bak

If the ~/.bash_profile file does not exist, the ~/.profile file will be read instead. This is the default on the Ubuntu Linux distribution.

If you want to copy the contents of the default ~/.profile file to a new ~/.bash_profile for editing, you can make a copy:

cp ~/.profile ~/.bash_profile

Note that ~/ in the Linux shell is a shortcut to the current user’s home directory.

Examples

Here are some things you can do with your Bash profile to get started.

Change the Shell Prompt

Usually, your shell prompt will look something like this:

linuxscrew@linuxscrew-host:~$

…a combination of your name, hostname, and the current working directory. This can be changed by modifying the .bash_profile file and adding the following line:

export PS1="?"

This will change the prompt to be a locomotive emoji. This on its own isn’t all that useful, but other things can be added.

This will change the prompt to the current logged in user, followed by the locomotive:

export PS1="\u ?"

You can add your own text, characters, and values. For a list of all of the built-in variables you can use (date, username, current directory, etc.), check out:

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt

Change The Text and Background Colours!

Building on the above example, we can add some color. This example will make the prompt text red with a blue background:

export PS1="\e[44;31m\u ? \e[0m "

…and this will add that the train emoji should blink:

export PS1="\e[44;31m\u \e[5m? \e[0m "

Note that:

  • \e[ : Starts formatting scheme
  • 44;31 : Background color and text color, respectively
  • \e[5m makes the train icon blink
  • \e[m : Stops the formatting scheme by resetting all attributes

There are a bunch of colors and formatting options you can use. This isn’t an exhaustive list, but it provides a few useful examples:

Code Meaning Example
1 Bolded “\e[1mBolded”
2 Dimmed “\e[2mDimmed”
3 Underlined “\e[4mUnderlined”
4 Blinking “\e[5mBlinking”
5 Inverted Colour “\e[7mInverted”

The available colors are numeric values that can be paired into background/text combinations. They will vary depending on your environment – this command will output the available combinations on your system:

for x in {0..8}; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo ""

Add Your Own Shortcuts / Functions

If you find yourself typing the same series of commands constantly, you can define a shortcut function in your bash profile. Adding the following code to the ~/.bash_profile file will define a new function that lists the directory, then exclaims how nice it is:

myShortcut(){
        ls -la
        echo "Look at that nice directory!"
}

To run it, you’d simply now need to run:

myShortcut

Into the shell and the contents of that function will be executed.

Applying the changes

To apply the changes to your profile and see them without restarting or closing the terminal, just run:

source ~/.bash_profile

What About Zsh?

We recently explored the zsh shell.

Zsh also has a profile file that serves the same purpose as the bash profile, located at:

~/.zprofile

It, too, can be edited just like the Bash profile, but with Zsh specific functions. Give it a go!

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment