book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!







How and where to start writing first Shell Script in UNIX Part 2

(Traditional format)

Tue Aug 31 16:14:20 2004 How and where to start writing first Shell Script in UNIX&
Posted by Rakesh Awasthi
Referencing: /Blog/B1073.html

This article is a follow up of my last article “How and where to start writing first Shell Script in UNIX”. It covers the shell variables, both User defined as well as system shell variable.


Hate these ads?

What is a variable?

A variable is a name associated with a value. It’s a string that can be used to represent and manipulate your data.

In UNIX shell script programming you need not declare a variable. You just assign a value to it. There are two types of variables:

  1. User shell variables
  2. System shell variables

User shell variables

To use a variable just initialize it, there is no need to declare it.

Myname=rakesh

Age=20






In the above two lines ‘Myname’ and ‘Age’ are two variables having ‘rakesh’ and ‘20’ as the value associated to them respectively.

Remember ‘20’ is a string not a numeric value and can’t be used for mathematical operations. I shall explain how to do mathematical operations in my follow-ups.

Declaring a Null String

A null string can be declared in any of the following three ways:

variable=

variable=””

variable=’’

Making a value Read-Only

I don’t want to change the value of my variable inside the UNIX shell script accidentally.


ad

readonly name=rakesh

Now the value of ‘name’ can’t be changed in your UNIX shell script. The read-only value can’t be even unset. I shall explain unset in the meantime. To get a list of all read-only variables type this on the prompt string

readonly

What is a valid variable name?

I wonder what a valid variable name is! What variable name should be given for better understanding of there usage and the associated value?

Only digits, alphabets and underscore ( _ ) are allowed in defining a variable name. Variable names are case sensitive, so Age and age are two different variables in UNIX shell scripts.

What is unset?

Unset erases all the reference of the variable from shell’s memory. To erase the value of variable ‘age’ type on the prompt string

unset age

This way you can use the same variable again in the program by associating a different value to it. Though you need not flush the value of a variable before reusing it but it’s a good practice to do show before associating a value of different data type to it.

System shell variables

Those who are familiar with the DOS shell might know what happens when you type set on the command prompt. It gives you the list of all environment shell variables. The environment shell variables are also known as system or public shell variables. Try doing the same with your UNIX shell. Type

set

on your prompt string and eventually you will get list of all system shell variables.

There are few commonly used system shell variables are

HOME

MAIL

PATH

EXINIT

PS1

PS2

USER

SHELL

TERM

You can use these shell variables in your program.

How to print or use the value of the variable

Now please tell me how can I use these variables in my UNIX shell script.

In fact it’s very simple. All you need to do is to put a dollar ( $ ) before your variable name.

$varname

will give you the value ‘varname’ is holding. And to print the value type

echo $varname




Comments
CommentsBlog1076 :

---September 1, 2004

Syntax correction:

Setting a variable to readonly in the Bourne shell is just:

readonly variablename

after variablename has been assigned a value.

( readonly variablename=value is allowed in other shells such as bash or ksh)

Bob

Click here to add your comments


Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
6213131 11,736

/Blog/B1076.html copyright August 2004 Rakesh Awasthi All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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.

Publishing your articles here

More:
       - Code
       - Linux
       - Unix
       - Programming
       - Shell
       - Unixware
       - Unix
       - Basics
       - Blog




Unix/Linux Consultants


http://www.schewanick.com SCO Unix, Solaris, Linx (various), PHP, MySQL, Apache, uniBasic, dL4, Perl, System Administration and more....


larryi@ccamedical.com SCO OS5, Debian Linux, RedHat Linux, MySQL, Apache, AJAX development using dXport/dL4/Unibasic, Windows Connectivity, Sharing Resouces, Automation, Shell Scripting


SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at Open Systems Computing and www.go2unix.com.



Twitter
o Banana and yogurt shake for lunch.. mmmmm 11:43 AM Mar 7th 2008




card_image







Change Congress