Simple bash script

From BITS wiki
Jump to: navigation, search
Go back to parent page Introduction to Linux for bioinformatics

A script

A script is just a plain text file. I will show this below. It contains written instructions, that can be understood by a programming language, in our case bash .

An example script

Few things to notice:

  1. in the script, we have defined 2 variables 'badday' and 'goodday'
  2. their values can be displayed by the program echo which takes as an argument the name of the variable preceded by a $ sign.
  3. the $USER variable, is an environment variable. They can be used in scripts. Env variables are typically written in capitals.

Getting more professional

We can make this easier. If you start your script with the symbol '#' and next specify the path to the interpreter, the terminal will feed this script automatically to the right interpreter for you! To see what this means, follow these steps.

Now we know the path to bash, we have to provide this path, on the very first line, preceded by #! (shebang or crunchbang). If you have another type of script, let's say perl, you find out the path to perl, and at this path behind a #! on the very first line.

By setting the shebang, the interpreter on the command line knows that this is a bash script!

To make it more readable, often the extension .sh is given to the text file. Note that this is not necessary! Linux does not define file types by extensions.

A good habit

This was our first bash script! I hope it was a painless experience.



Go back to parent page Introduction to Linux for bioinformatics