What is a Bash profile and how to edit one's profile to do useful stuff
Typically we interact with the Linux operating system through what is referred to as a shell interpreter. A common interpreter is the Bourne Again Shell, which people call by the catchy name of bash
(you will also come across zsh
and others). Think of the relationship of the Shell and Linux like that between your steering wheel and your car. The steering wheel is not the car, just as bash
is not Linux. Rather the steering wheel lets you interact with your car in ways that make the car useful, just like typing commands into the Shell lets us make Linux do our bidding.
When one opens up a new Terminal window, we are presented with a prompt for typing commands that will be interpreted by the shell. We can configure and customize the shell by specifying settings in a text file. For bash
one of the configuration files that you can modify is ~/.bash_profile
. This is a text file that includes Bash variables commands (the ~/
indicates that it is located in one’s home directory). Bash commands are simply those commands that one can type into the Bash Shell (all the stuff we normally use is fair game, like ls
or cd
, but there are ways to make the program more useful than printing the contents of one’s home directory to STDOUT, see below).
Note the period that starts the file name (“.bash_profile”). This means the file is typically not listed with ls
or visible in the file browser. To see all files, including those that start with .
, use ls -a
. These invisible files typically hold info that lets one customize some aspect of the computing experience.
If you don’t have a bash profile in your home directory, don’t worry. Simply make and edit one using an editor such as nano: nano ~/.bash_profile
Once you have written your spiffy new Bash profile you either need to open up a new Shell or source the settings. To do the latter, type source ~/.bash_profile
Useful configurations
Ok, so what useful things can we put in a Bash profile? The answer is anything one can think of that makes doing computing easier. Here are a few simple examples:
alias – It is cumbersome to type lengthy commands over and over. Instead make a new command using an alias!
Consider this option:
alias interact="salloc --account=microbiome --time=1:45:00 --nodes=1 --ntasks-per-node=1 --cpus-per-task=1 --mem=2000”
. If I add this alias into my Bash profile, then the next time I open up a new Shell I can simply typeinteract
on the command line and the computer interprets this as requesting an interactive session with the aforementioned specifications. Pretty handy.alias ll="ls -laF"
alias ls="ls -F"
alias rm="rm -I"
# make rm interactive
alias mv="mv -i"
# make mv interactive
alias cp="cp -i"
# make cp interactive
set environmental variables – one can set or modify environmental variables for the shell. For instance, if one wants to change the $PATH then this can be specified in the Bash profile. See this link if you don’t know what a $PATH is: http://www.linfo.org/path_env_var.html
PATH=$PATH:~/bin:.
# append ~/bin and ./ (current directory) to the PATH
modified environmental variables need to be exported at the end of the configuration:
export PATH
Lots and lots of recipes exist for putting useful things in a Bash profile. Do some googling and then get creative. You will likely find lots of convenient ways to improve your computing experience through modification of the Bash profile.
Note that if you edit your profile in Nano to save and exit the program see the instructions at the bottom of the editing window.