APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

How and where to start writing first UNIX Shell Script

© August 2004 Rakesh Awasthi

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 queries.

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 scripting.

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 executes them.

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 working in?

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

echo $0

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

cd directory_name

To get to know about your present working directory simply type


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 using ./first

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 program.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> How and where to start writing first UNIX Shell Script


Inexpensive and informative Apple related e-books:

Take Control of Preview

Take Control of IOS 11

Take Control of Numbers

Take Control of Automating Your Mac

Take Control of Parallels Desktop 12

More Articles by © Rakesh Awasthi

In this site i was looking for some Unix Shell scripts
for beginners and i didnt find any of them .
I think The site should include some examples of the
shell programing.


---October 1, 2004

OK. I don't LIKE shell programming, because shells are so pathetically bad at it, but maybe I or someone else can grit our teeth and throw a few out :-)

But there are sites really devoted to this: https://shelldorado.com/ for one..


Fri Feb 25 20:50:22 2005: 79   anonymous

I wrote a script that queries some files and redirects the output into a file, but the problem I am having is that the script never stops running. I have to hit CTRL C, CTRL C to exit back the the prompt.

Is there some command in the script that will bring me back to the prompt?

Fri Feb 25 23:06:23 2005: 80   TonyLawrence



Usually followed by 0 if it's a "good" exit:

exit 0

or a non-zero number if you want to indicate some error:

exit 1

That lets it be used like

yourprog || echo "it didn't work"


yourprog && echo it did work


Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby. (Kent Beck)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode