Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 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 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 Shell window (the Terminal utility on the Mac, for instance) a program is run in the background unbeknownst to us. This program is the Bash profile and is called “.bash_profile”. This is a script that includes Bash commands and 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 program file name (“.bash_profile”). This means the file is invisible. If one types ls in one’s home directory any files that start in a period will be invisible. To see them, one must provide the -a flag to ls, like this, “ls -a”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 touch, like this: “touch .bash_profile”. For this to work properly, you need to be in your home directory, because that is where your computer expects to find the “.bash_profile” program. You can edit the bash profile by opening it with nano (type “nano .bash_profile” onto the command line). 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 type

...

    • interact 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. Also, to use your spiffy new Bash profile you either need to open up a new Shell or source the program. To do the latter, simply type “source .bash_profile” (a path to the profile can be provided, one doesn’t have to be in one’s home directory to source the program. Source just means to run a program)