Open a new file in your favorite text editor and save it in any name of your choice but with the extension
.sh. For example, let us name it
shfile extension is not mandatory but is a nice way for us to later remember that this file is a script.
Now in this file (myfirstscript.sh), enter the following line.
This line should always be entered the first thing in any bash script you create. It lets the shell know where to find the bash program.
In some Linux distributions the bash command is located at
When in doubt, use the
whichcommand to find the location as follows :
$ which bash /usr/bin/bash
# Continuation of the above script ... # Now lets enter a few commands in this script ... ls -l sleep 2 who sleep 2 w
Nothing spectacular here. Note that
sleepis used to pause the execution process for certain time in seconds. Next fire up a console (xterm, gnome-terminal ...) and set the executable bit for the script
myfirstscript.shas follows :
$ chmod u+x myfirstscript.sh
This lets you execute the script by its name as follows :
All this may seem easy.
But you can get added benefits if you substitute
bashwith Python language to write your scripts.
Noah Gift provides compelling reasons to pick up Python programming language skills and start writing your scripts in this language.
If you do not know Python language and is looking for some direction then you should look at the two books Core Python programming and Python phrasebook which will give you a head start in mastering this powerful but easy to learn language.