BASH scripting

From Rizzo_Lab
Revision as of 11:19, 24 January 2020 by Gduarter (talk | contribs) (Environment Variables)
Jump to: navigation, search

Bourne-Again Shell (Bash)

Bash is an acronym for "Bourne-Again Shell", the name of a code interpreter and a high-level programming language, and it is a must-know tool in Computational Chemistry and Biology. You can use Bash scripting in Unix/Linux computers through a terminal. When you initialize the shell, i.e, the interpreter, your computer runs initialization files -- ~/.bash_profile, ~/.bash_login, and ~/.profile (where ~/ points to your home directory) -- but we do not recommend changing these files unless you really know what you are doing. In most cases, you can change the ~/.bashrc file, which allows the user to customize the system according to their needs.

A bash script is a text file containing a series of instructions written in the bash language. You can create one by typing the following commands in the terminal:

 touch my_first_script.sh

which will generate a modifiable file that you can use to write the instructions to be executed by the shell. You can use the Vi text editor to write your code; just remember to add to the beginning of the file the following line:

 #!/bin/sh

This line tells the interpreter that this is a bash script. You can run your script by telling the interpreter:

 bash my_first_script.sh

or you can change the permissions of the file to make it an executable by typing:

 chmod +x my_first_script.sh

and then running:

 ./my_first_script.sh

Suppose your my_first_script.sh contains the following lines:

 #!/bin/sh
 
 number=6
 for ((i=0;i<number;i++))
 do
     echo "Hello world ${i}"
 done

If you run ./my_first_script.sh, the output will be:

 Hello world 0
 Hello world 1
 Hello world 2
 Hello world 3
 Hello world 4
 Hello world 5

For more on commands, see Unix.

Environment Variables

Declaring and accessing variables

Bash allows the user to assign values to variables in the command line, but it is more common to set any variables inside your scripts or ~/.bashrc file. In Bash, you define your variable using the following syntax:

 my_variable=value

Do not leave spaces between the variable name and its value. You can check the value of a variable by typing the following command in your terminal:

 echo $my_variable

The shell will show the following result in your screen:

 value

Remember to tell the shell that you want to return the value of the my_variable by using the $ sign, otherwise, you'll be shell to print the string my_variable on the screen.

In the previous section, the variable number contained the value 6, and the variable i was an iteration counter that was called inside the for loop. It is good practice to encapsulate the variable name with curly brackets {} to avoid ambiguities inside the code.


Paths and Environment variables

Variables such as HOME or PATH are inherited from the environment. They have short and easily memorizable names that characterize important environment features. HOME is the variable that identifies your home directory (e.g, /gpfs/home/your_username on Seawulf). PATH is the variable that determines which directories the shell should look for the programs that the user might use.

HOME and PATH contain path information, i.e., the "addresses" within the directory tree that allows the user to find their files and executables. If you use the Python programming language, you might need to create a PYTHONPATH variable to be able to include your own Python subroutines and classes. Similarly, if you use the Amber software, all Amber-related programs can be found in the path defined by AMBERHOME.

Basic commands

Your .bashrc file