Korn Shell Variables

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.

ADVERTISEMENTS

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

  1. Korn shell variable names can begin with an alphabetic (aā€“Z) or underscore character.
  2. 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/mysql

Store date and time:

NOW=$(date)

Set file path

HTTPDCONF=/etc/httpd/httpd.conf

A 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=foo

ksh: BASE: is read only

ksh shell programming, unix ksh variable,ksh positional parameters,ksh variable substitution,bin ksh,ksh shell script

šŸ§ Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Unknown Jan 31, 2013 @ 7:07

    thanx “_”

  • joseph Feb 12, 2013 @ 9:32

    My command goes like this in Ksh

    filee =$(ls -lrt filename|tail -1|cut -d” ” -f1)
    cat $filee

    or
    filee =`ls -lrt filename|tail -1|cut -d” ” -f1`
    cat $filee

    However i am not able to get the required output.

    • simon Jul 3, 2013 @ 13:57

      @joseph, there must not be any spaces around the “=” on the command line:
      filee=`ls -lrt filename|tail -1|cut -dā€ ā€ -f1`

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.