Contents[Hide]
After reading this short shell scripting tutorial you will be able to to create and execute your own bash shell script. Not previous knowledge of shell scripting is required. However, you are expected to have some knowledge of hot ot start terminal and how to edit text files with some text editor of your choice.

1. Writing your own first Shell Script

In this section and as well as a first step we are going to create a simple shell script doing nothing else but printing "Shell Scripting" on the terminal output. This will will your starting point. To your first basic script together you need to know two things:

1.1. How to execute command

Since the shell script is nothing else just collection of commands, before we can write any shell script we need to know how to execute commands on in the terminal. Open your terminal and type echo Hello disregarding the "$" sign at the beginning of the line which only simply indicates that you supposed to type command as a non-privileged user:
$ echo Hello
Hello
In the above example you have type command echo which simply prints anything passed to it. In this case we passed string "Hello" and thus our string we printed on the second line.

1.2. What is an Interpreter

What is interpreter? In simple word the interpreter is the process behind script execution capable of interpreting all commands within your shell. Since we are writing shell script we can choose from number of shells to interpret our commands available on the Linux system, however in our case we choose the most often default shell BASH ( Bourne-again shell ). First, let's locate our BASH shell interpreter using which command to reveal a location of BASH interpreter on your system:
$ which bash
/usr/bin/bash
Now we are ready to write our first BASH shell script. Open your favorite text editor and enter the following code:
#!/usr/bin/bash 
echo "Shell Scripting"
Note that first line contains our interpreter which is indicated by the fact that is is located on the first line of our script as well as that it is prefixed with #! symbols. On the second line we have typed our already know command echo followed by string to be printed out. Save this file in your home directory using name bash-script.sh

2. How to Execute Shell Script

Now we are ready to execute our fist script. There are two ways how to execute shell script from a command line. First, and the most simplest way is to prefix our new bash script file name with bash command. In this case not execution bit are required to be present:
$ bash bash-script.sh
Shell Scripting
The other and more common way to execute shell script is to make script executable and simply execute it by specifying full path of the script or prefix the script name with ./ if the script is located in you current working directory. First make you script executable using chmode command:
$ chmod +x bash-script.sh
At this point you are ready to execute you BASH script either specifying the full path to the script. For example:
$ /home/lubos/bash-script.sh
Shell Scripting
Where, "lubos" is the name of my home directory. Another way to execute our new BASH script is to prefix it's file name with ./:
$ ./bash-script.sh
Shell Scripting
Congratulation, you have now successfully executed your first BASH script. If you are up to more advanced bash shell scripting visit the following Bash Scripting Tutorial

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top