A Profile of A Bash Profile

Something that came up time and time again when I was learning to code was the bash_profile. It was often suggested in tutorials and courseworks to add something to my bash_profile, for example. Nobody ever seemed to have a good explanation about it, and every time I asked somebody what this meant or how to do this I would get 1 of 2 responses:

  1. "I'll do it for you"
  2. Here's some confusing manpage or blog post that 'explains' it.

Neither of these 2 responses actually taught me what was going on, why I needed to do this, or how to do it myself. This blog post hopes to explain why a bash profile exists, what it is and how you can use it.

What actually is a bash profile?

A bash profile is actually just a file that is used by the bash shell. This file is loaded when bash is invoked as a shell (e.g. you open the Terminal), and any commands that are inside that file are executed. You can essentially add custom configuration and preferences to this file which may improve your experience when using the command line.

This file is usually hidden, for example if you are searching for it using the finder on Mac, as it begins with a dot (.). Files starting with dots (dot files) are automatically treated as hidden both in the Finder and when using ls from the Command Line. You can see the dot files present in a directory by using the ls -a command instead of the usual ls.

Why does it exist?

Often when you interact a lot with the command line, you will want to create 'aliases' which will make common tasks easier to complete regularly. An alias is just when you want to use a different name for something. For example, if I often type cd ../../.., it'd probably be easier to type ..3, so I can set an alias for this in my bash_profile:

alias ..3='cd ../../../'

The bash profile can ensure these aliases are always exported, and are therefore usable whenever you use the terminal. Another useful alias example is one which can delete all your git branches which have been merged into the current checked out branch. This will tidy up your local git repo for a project:

alias git-remove-merged='git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d'

Yet another useful example is modifying key bindings in your terminal. This means you can make combinations of different keys on your keyboard do useful things. For example, the following command will mean that when you use alt and the left or right arrow keys, you will skip forward or backward a whole word:

bind '"\e\e[C": forward-word'
bind '"\e\e[D": backward-word'

You can also use the bash profile to set environment variables which will be useful if you often edit files using the command line, for example. If you want to set your default editor to Visual Studio Code instead of something like vim, you can say:

bindkey "^[^[[C" forward-word bindkey "^[^[[D" backward-word

export EDITOR="code"

This would obviously be very useful to somebody who has to deal with the command line a lot, especially if they have to complete repetitive tasks.

You can also add functions to your bash profile. This is useful if you have a set of commands you run together regularly, and would like to only have to type one command instead. A common example is combining mkdir (creating a new directory/folder) and cd (moving into that directory/folder). You could add the following function to your bash profile in this case:

make_and_move () {
 mkdir -p $1
 cd $1

$1 in this case is the first argument to this method. If I typed make_and_move my-application, then the string 'my-application' would be the first argument in this case, and so the result would be that you would have created a new directory called my-application and moved into it.

How can I use it?

To add something to your bash profile, there are just a few steps:

  1. Open the Terminal

  2. Run code .bash_profile (if you use Visual Studio code, you can also use atom .bash_profile or vim .bash_profile if you use Atom or vim respectively).

  3. Modify the file as you wish.

  4. Save your changes.

  5. These changes will either be applied the next time you open the terminal, or if you run source ~/.bash_profile.

That's it! I hope this post was useful!