A Quick Guide to .bashrc

What is .bashrc?

There are many different types of shells which can be used by Linux terminals. By default, Raj uses the Bourn Again SHell, or bash for short. Upon logging into Raj, several login scripts are run which load a user's personal preferences. One these such scripts is .bashrc. Upon creation of their account, a .bashrc file with default settings is copied into a user's home directory. The user can then modify that file to customize their session. They can modify environment variables, load modules, create aliases and activate Python virtual environments. Users can also modify .bashrc to change aesthetic things like the terminal color scheme and what their bash prompt displays. To edit your .bashrc file use a command line editor like vim or nano:

vim ~/.bashrc
nano ~/.bashrc


Modules, Environment Variables and Python Environments

Since .bashrc is just a shell script, any bash command you would issue to your bash prompt can be placed inside .bashrc. This includes loading modules, modifying environment variables and activating Python virtual environments. For example to load a module you can add the line:

module load <module>


To modify an environment variable, like PATH, add the line:

export PATH=$PATH:<path/to/dir>


Activating a Python environment is as simple as adding the line:

source <path/to/env>/bin/activate


Aliases

Aliases are exactly like what they sound like. When a person has an alias, it means there are two separate names which refer to the same person. When a command has an alias it means there are two separate commands which take the same action. Aliases are often created to make commonly used commands shorter. Aliases can be added directly to .bashrc, however, it is common practice to add aliases to a file called .bash_aliases, then load .bash_aliases into .bashrc like so:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi


An alias can be defined using the following syntax:

alias <aliasname>="<command>"

 

For example, a common alias is to define ls -l as ll by adding the line:

alias ll="ls -l"


Other common aliases include:

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias la='ls -a'
alias rm='rm -i' #-i prompts user before deletion
alias cp='cp -i' #-i prompts user before overwriting


Another alias that may be helpful is activating Python virtual environments. Say you have a multiple Python virtual environments you want to switch between quickly. Instead of loading the environments by default in .bashrc, you could create aliases to activate them for you. For example, say you have three Python projects  project1, project2 and project3, and inside each project you have an  env directory containing your Python environments. You could create three aliases to activate each environment like so:

alias env1="source ~/project1/env/bin/activate"
alias env2="source ~/project2/env/bin/activate"
alias env3="source ~/project3/env/bin/activate"


Now if you want to quickly switch between environments you can issue the following commands:

deactivate # deactivate the currently loaded environment
env2 #activate environment for project2


Additional Resources