Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 








Shell Scripts Seem Like Magic at First

Linux Tips by Burleson Consulting

Programming with Linux

In this chapter, additional Linux commands will be introduced and used to show how to build programs called shell scripts.  Shell scripting is the topic of my next book, Oracle Shell Scripting: Linux and UNIX Programming for Oracle in which I cover much of this information in great detail.  If you are interested in shell scripting you might want to pick it up!  Though some of the examples use Oracle, much of the books content is applicable for anyone interested in the topic.  Here we will just cover things briefly in order to give a general idea about how scripts are created to perform functions that are repeated on a periodic basis.

Instead of jumping directly into the creation of scripts, there are a few additional topics that should be presented first as well as some questions that need to be answered.

What is a shell?

As described earlier in this book a shell provides a command interpreter environment for giving instructions to the Linux kernel via English-like commands.  There are a number of different shell environments available in Linux.  In order to determine which shell environments are available on a user?s version of Linux, the following command should be issued:

$ cat /etc/shells

The resulting list indicates that there are several shells available on this server such as the Bourne shell (sh), the Bourne Again shell (bash), the Korn shell (ksh) and the C-shell (csh), among others.  In Linux, the bash shell is normally the default shell.

To determine the default shell to which the system is currently set, the following command should be run:

$ echo $SHELL

As predicted, the bash shell is the current default.

The shell is a command language interpreter that executes commands read from the standard input device such as a keyboard or from a plain text file.

A shell can be compared to the Microsoft Windows command prompt, which also executes commands issued from the keyboard or from text (.bat) files; however, the shell environment is much more robust and feature-rich than its Windows counterpart.

Command Aliases

Aliases are the shell scripts little cousin.  A shell script, as we will see shortly, is a file which contain a list of commands.  Aliases, in contrast, can be used to perform complex commands but are set in the shell like environmental variables.

If you find yourself frequently using a fairly complicated command like the one below and want to be able to run it without typing the whole thing you can use the alias command to set up a shortcut.

$ alias alert=?tail -200 /u01/installed/software/mypgm/alert.log|more?

Now you can execute this whole command simply by typing alert.  Many of the features we will discuss when talking about shell scripting are available in aliases including the pipe (|) used to send the output of one command to the input of another.

A small set of well written aliases can save you a lot of keystrokes, just be careful not to make aliases with the same name as commands you use unless you want the alias to replace that command when you type it.

Not just any clown can learn Linux shell scripts

Why use shell scripts?

Since the shell is capable of reading commands from a file, called a script, it becomes a simple task to use a basic text editor like vi to build scripts to simplify frequently performed tasks and to automate the performance of those tasks. 

Programming simple or complex actions into shell scripts can simplify repetitive administrative and maintenance functions within the Linux environment.

Getting Started with a Simple Shell Script

A shell script gets executed just as if you were at the keyboard typing commands.  Because of this we will demonstrate many of the capabilities of shell scripts right at the command line.  Once tested at the command line, these commands can be moved into shell scripts.  Here's an example of a simple shell script:

# A simple shell script to output the time
echo "The current date and time is `date`"

This three line script starts with two comments (anything following a # is ignored by the shell), then has an echo command which outputs some text and the current date with the date command.

If you enter these lines into a text file called youl can then execute it as a shell script with the bash command:

$ bash
The current date and time is Fri Sep 15 14:58:23 EDT 2006

The .sh extensions is not necessary but is the conventional way to indicate a bash shell script.

If we want to make things even easier we can change the execution privileges on this shell script so we don't even need the bash command to run it.

$ chmod u+x
$ ./
The current date and time is Fri Sep 15 15:18:56 EDT 2006

This works because the first line of our shell script is special.  When the first line of a shell script begins with #! it is used as a hint to indicate what command should be used to execute this script.  When Linux processes this script for execution it sees this hint and uses bash to execute it.

We'll spend the rest of this chapter looking at many of the hundreds of commands you can use in shell scripts, but remember, shell scripts don't have to be complicated!  They may just take the place of a couple commands that you run frequently or a long command that you have troubles remembering.

Shell variables

Shells provide an abundance of useful built-in information that can be referenced in globally available variables.  In order to see the information provided in a shell, the set command can be run as demonstrated below.

Here's a partial output of the set command:

$ set
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
SSH_CLIENT=' 1379 22'

The contents of a shell variable can be displayed by using the echo command and prefacing the variable name with a dollar sign as demonstrated below. Shell variables are referenced using all capital letters.

$ echo $TERM
$ echo $USER
$ echo $HOSTNAME ... $LOGNAME ... tclark

There are also some special built-in variables that can be useful when creating shell scripts. Some of them are listed in Table 8.1 below.

Built-in Variable



The total number of arguments passed to a shell script on the command line.


All arguments passed to the shell script.


The command (script) invoked on the command line.

$1 - $9

The first through ninth arguments passed to the shell script from the command line.

Table 8.1: Shell built-in variables

These variables are provided by the shell, but when programming shell scripts you may need additional variables.  Next we'll look at variables you can make yourself, user defined variables.

User Defined Variables

Shells also allow the creation of variables for use within scripts (local variables) and for passing between scripts (global variables).  User variables are traditionally created using lower-case characters though they can be any case you want.

The creation of a variable requires merely choosing a lower-case name for the variable and giving it a value using an equal (=) sign.  There should be no spaces on either side of the equal sign.  If there are spaces or special characters in the contents of your variable you should enclose the variable in single quotes.

The unset command can be used to nullify the value of a previous set variable.  The following are some examples:

$ myname='Terry Clark'
$ echo myname
$ echo $myname
Terry Clark
$ unset myname
$ echo $myname

$ number=10
$ echo $number

In this example we see that in order to set the contents of a variable we just give the variable name, but if we want to retrieve the contents of a variable we must use a dollar sign ($) before the variable name.

The variables created above are local variables available only to the current shell.  Variables must be exported if they are to become global so they can be referenced by other shell scripts.  The following example shows the creation of global user variables.

$ myname=Terry
$ export myname

In the bash shell we can combine these two lines into one and use the following command to set and export a variable:

$ export myname=Terry

Variables are great for storing information.  Later we'll see how we can use variables to make decisions and loops, but first we've got a little more basic ground to cover.

Evaluating Expressions

Shells can evaluate complex expressions and allow the user to perform math functions.  The following examples demonstrate some simplistic math using the expr command:

$ expr 6 + 3
$ expr 6 + 3 + 8
$ expr 8 - 2
$ expr 12 / 3
$ expr 15 \* 3
$ expr 20 % 3
$ echo `expr 15 + 3`

The example above shows not only the common use of expr but some of the exceptions you may find in the expr command, such as:

The multiplication example uses an escape character (\) before the * so that the * is not interpreted as a wildcard symbol.

?20 % 3? is read as 20 mod 3 and yields the remainder when 20 is divided by 3.

The last example uses backward quote signs (`) to enclose the command.  The command within the backward quotes is executed and the result echoed to the screen.

This is an excerpt from "Easy Linux Commands" by Linux guru Jon Emmons.  You can purchase it for only $19.95 (30%-off) at this link.



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational