Simple bash script
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
Create a text file named 'buddy' in your home with following content:
badday="Cheer up" goodday="Doing great" echo "$badday, $USER !" echo "$goodday, $USER !" |
One way of doing this is:
nano buddy and copy of the contents of the header above. |
Save the contents by pressing <ctrl>+O |
Close nano with <ctrl>+x |
What type of file did you create? |
file buddy
buddy: ASCII text |
That file contains plain text. |
To execute the commands in that file, feed it as an argument to the program 'bash'. |
bash buddy Cheer up, bits ! Doing great, bits ! |
Few things to notice:
- in the script, we have defined 2 variables 'badday' and 'goodday'
- their values can be displayed by the program echo which takes as an argument the name of the variable preceded by a $ sign.
- 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.
Find out the path to the program bash |
which bash /bin/bash |
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.
Open the text file 'buddy', and add at the start of the file '#!' followed by the path to bash: |
nano buddy ... edit the text cat buddy #!/bin/bash badday="Cheer up" goodday="Doing great" echo "$badday, $USER !" echo "$goodday, $USER !" |
Before turning the text file into a script, set the execute permission (to allow execution) with chmod |
chmod +x buddy |
What type of file is your script? |
file buddy
buddy: Bourne-Again shell script, ASCII text executable |
By setting the shebang, the interpreter on the command line knows that this is a bash script!
Now run your script as if it were a program (./<script_name>) |
./buddy Cheer up, bits ! Doing great, bits ! |
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.
Rename your script to 'buddy.sh' |
$ mv buddy buddy.sh |
Alternative (less typing!) $ mv buddy{,.sh} |
A good habit
The last line of your script should be 'exit 0'. If bash reaches this lines, it means that the script was successfully executed. |
Add it by opening the file with 'nano' and modifying its contents.
$ cat buddy.sh #!/bin/bash badday="Cheer up" goodday="Doing great" echo "$badday, $USER !" echo "$goodday, $USER !" exit 0 |
Alternative. Less typing!
echo "exit 0" >> buddy.sh |
This was our first bash script! I hope it was a painless experience.
Go back to parent page Introduction to Linux for bioinformatics