How and where to start writing first UNIX Shell Script
Posted by Rakesh Awasthi
If you are new to UNIX and having not much idea of UNIX, worried
how to start writing shell scripts in UNIX, wondering why we need
UNIX shell programming, etc. then this article will answer all your
The article doesn't cover the shell scripting it only gives a
brief idea about what is UNIX shell and how and where to start
writing first shell script. I shall cover variable, expression,
control structures, function, file handling, awk programming,
debugging one-by-one in my subsequent articles on the UNIX shell
What is a UNIX shell?
I have worked on Windows 3.x/9x/2000/NT and even on XP, but I
have never heard of a shell. What is it and why does every
conversation on UNIX starts with the UNIX shell (of course security
is also a hot topic)?
Those who have worked on any flavor of Windows, they would be
familiar with the C:\>. It's a prompt, Command prompt. What it
does for you? It allows you to enter some requests that you want
from Operating System to perform.
In the same fashion the UNIX shell is a command interpreter that
reads the command given by you as a user of the UNIX shell and
Which shell am I using?
Why is this question first of all? I just logged into my UNIX
system and started working and now I want to write my first shell
script. Is there any other shell available than the one I am
Guess how many UNIX shell are available. There are more than 30.
Though sh, bash, csh, ksh, tcsh are the shell that you will find
more often. These shells are convenient to use to you as a user and
as a programmer.
These shells are different by each other in the user privileges,
commands and in providing different interactive features.
To know which shell you are using type
(Article continues after the break)
on the shell prompt string.
Why should I use shell for programming?
I am very good in C and/or Perl and can write hundreds of lines
of code in a day? Then why do I need to write my programs or
scripts in the shell scripts?
Yea, it's very true that era is to use graphical interfaces. No
doubt. But for the simple file manipulation, automating tasks,
writing system administration tasks writing a shell script is
quickest and simplest bet.
Where to write my first UNIX shell script?
Okay, I am becoming a little bit impatient and want to start
with my first shell script. I have just logged in. where am I?
Where should I write my first shell script?
The directory where you find yourself immediately after doing
login is your home directory. To change the directory type this on
the shell prompt
To get to know about your present working directory simply
You can start writing your shell scripts in any directory. You
would be able to execute your script by giving the respective path
to it from other directory also.
Creating first shell script
You can start writing your first shell script program at the
prompt string itself. But that won't be available to you after its
execution. Moreover, you will not be able to debug your program in
case you find errors (that is not very surprising at early stage).
So I would suggest you to use some kind of text editor. vi is the
most common used editor for this purpose.
What should be my first line?
Every shell script must start with the following line
The line is called a shebang line due to first two
characters(#!) in the line. # is sometimes called as sharp
and ! as bang. This tells the UNIX to execute the script
with the correct interpreter.
Classic "Hello World!" Program
Let's start with the classic "Hello World" program. To get the
desired output type
echo "Hello World!" save file and try to run it
Why is my script not running?
What went wrong? I did the same that you have said and it's a
one line script only.
Don't worry. Nothing is wrong with your shell script. The file
is not an executable file. You need to make it executable by
writing simple command.
chmod +x first
Now try doing ./first you will get the expected output.
How to comment something?
I am the only one who is going to use this script. Why should I
bother to write comments?
This may be true, but after writing many shell scripts and then
coming back to your first one? Believe me, sometimes it would not
be easy for you to understand your own program. It's a good
practice to put comments for the better understanding of the
If this page was useful to you, please help others find it:
More Articles by Rakesh Awasthi
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Jump to Comments
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.