What is a shell variables? How do I define and use a variable under korn shell?
A variable is nothing but a symbolic name associated with a value and whose associated value may be changed.
Variables is used by the Korn shell to store values. Setting variables within a script or at a shell prompt is pretty simple. Define a variable called IP and BASE as follows:
IP="foo" BASE="/nas/backup"
To display variable, use:
echo $IP echo $BASE
Rules For Naming Korn Shell Variables
- Korn shell variable names can begin with an alphabetic (a–Z) or underscore character.
- Other variable names that contain only digits (0–9) or special characters (!, @, #, %, *, ?, $) are reserved for special parameters set directly by the Korn shell.
How Do I Create a Null Variable?
Simply use the following syntax:
DIR=If a value is not given, the variable is set to null.
Common Shell Variable Examples
Set backup directory path:
BACKUPROOT=/iscsi/mysqlStore date and time:
NOW=$(date)
Set file path
HTTPDCONF=/etc/httpd/httpd.confA Sample Shell Script Using Variables
#!/bin/ksh NOW=$(date) DNSCONF="/etc/resolv.conf" SERVERNAME=$(hostname) OS=$(uname) echo "Your OS : $OS" echo "Server Name : $SERVERNAME" echo "Current date and time : $NOW" echo "Client DNS Config file - $DNSCONF" echo "----------------------" cat $DNSCONF
typeset Command
Use typeset to define variable attributes. Some common examples:
Set the type of variable to be integer
typeset -i x=10 typeset -i y=10 typeset -i z=$(( $x + $y )) echo "$x + $y = $z"
Set the type of variable to be float
typeset -F PI=3.14 echo $PI
You can also use it as follows:
typeset -F3 PI=3.14159 echo $PI
How Do I Store Command Output To a Variable?
Variables can be assigned command output using the following format:
variable=$(command)
or
variable=`command`
For e.g. store hostname to a variable called HOST, enter:
HOST=`hostname` echo $HOST
OR
HOST=$(hostname)
How Do I Define Read Only Variable?
Set the readonly attribute using typeset command:
typeset -r BASE=/iscsi
If you try to make change, you will be greeted with an error message:
BASE=fooksh: BASE: is read only
ksh shell programming, unix ksh variable,ksh positional parameters,ksh variable substitution,bin ksh,ksh shell script
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -

