How to Use Korn Shell (ksh) Variables in Linux
Introducing Korn Shell Variables
A variable refers to a temporary storage area in memory. Variables contain:
- Information needed for customizing the shell
- Information needed by other processes to function properly
The shell enables you to store values in variables. Korn shell programming uses two types of variables:
- Variables that are exported to subprocesses
- Variables that are not exported to subprocesses
The table below describes the Korn shell commands used to set, unset, and view variables.
|To set a variable||VAR=value export VAR=value|
|To unset a variable||unset VAR|
|To display all variables||set, env, or export|
|To display values stored in variables||echo $VAR|
When a shell variable follows the $ sign, the shell interprets that the value stored inside that variable is to be substituted at that point.
Referencing Values in Variables
You can use the echo command to display the value that is stored inside a shell variable. For example:
$ echo $SHELL /bin/ks
To list all shell variables with their current values, perform the set command. For example:
$ set DISPLAY=:0.0 EDITOR=/usr/bin/vi ERRNO=13 FCEDIT=/bin/vi HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help HOME=/export/home/user1 HZ=100 IFS= LANG=C LINENO=1 LOGNAME=user1 MAIL=/var/mail/user1 MAILCHECK=600 MANPATH=/usr/man:/usr/openwin/share/man OLDPWD=/export/home/user1 OPENWINHOME=usr/openwin PATH=/usr/openwin/bin:/bin:/usr/bin:/usr/ucb:/usr/sbin PPID=596 PS1=’$ ’ PS2=’> ’ PS3=’#? ’ PS4=’+ ’ PWD=/tmp SHELL=/bin/ksh TERM=dtterm TERMINAL_EMULATOR=dtterm TMOUT=0 TZ=MET USER=user1 _=set office=/export/home/user1/office private=/export/home/user1/private
To make the value of a variable known to a sub-shell, export it using the export command. To view a list of all these variables and their current values, perform the export command:
$ export DISPLAY=:0.0 EDITOR=/usr/bin/vi HELPPATH=/usr/openwin/lib/locale:/usr/openwin/lib/help HOME=/export/home/user1 LANG=C LINENO=1 LOGNAME=user1 MAIL=/var/mail/user1 MANPATH=/usr/openwin/share/man:/usr/man OPENWINHOME=usr/openwin PATH=/usr/openwin/bin:/bin:/usr/bin:/usr/ucb:/usr/sbin PWD=/etc SHELL=/bin/ksh TERM=dtterm TERMINAL_EMULATOR=dtterm TZ=MET USER=user1 _=set office=/export/home/user1/office
Setting and Unsetting Shell Variables
A variable is set and a value is assigned with the following syntax:
There is no space on either side of the equal sign (=). For example:
$ private=/export/home/user1/private $ set| grep private private=/export/home/user1/private $ cd $private; pwd /export/home/user1/private
To make the value of a variable known to a sub-shell, use the following command syntax:
$ export VAR
$ export var
$ export office=/export/home/user1/office $ echo $office /export/home/user1/office
You can delete the values stored in shell variables with the following command syntax:
$ unset VAR
$ unset var
For example, to unset the variable private, perform the following commands:
$ unset private $ echo $private $
The output of the echo command is a blank line.
Displaying Default Variables
The table below describes variables that are assigned default values by the shell on login.
|EDITOR||Defines the default editor for the shell.|
|FCEDIT||Defines the editor for the fc command. Used with the history mechanism for editing previously executed commands.|
|HOME||Sets the directory to which the cd command changes when no argument is supplied in the command line.|
|LOGNAME||Sets the login name of the user.|
|PATH||Specifies a colon-delimited list of directories to be searched when the shell needs to find a command to be executed.|
|PS1||Specifies the primary Korn shell prompt ($).|
|PS2||Specifies the secondary command prompt (>).|
|SHELL||Specifies the name of the shell (that is, /bin/ksh).|
Customizing Korn Shell Variables
This section describes how to customize Korn shell variables.
The PS1 Prompt Variable
The shell prompt string is stored in the shell variable PS1, and you can customize it according to your preferences.
$ PS1="[email protected]`uname -n` \$PWD $ " [email protected]: $
In this example, the prompt displays the login name of the user the system’s hostname and the current working directory. The username is read from the variable LOGNAME, and the hostname comes from the output of the ‘uname -n’ command. This prompt displays the correct information even when the user logs in on different hosts. The back quotation marks delimit an imbedded command string The following example shows how the value of another variable is used for prompt deﬁnition.
$ ILU=”I like UNIX” $ PS1=”$ILU >” I Like UNIX > echo $ILU I like UNIX I like UNIX >
The PATH Variable
The PATH variable contains a list of directory path names, separated by colons. When you perform a command on the command line, the shell searches these directories from left to right to locate that command. The shell executes the ﬁrst command that it ﬁnds. If the shell does not ﬁnd the command in any of the listed directories, it displays the following error message:
ksh: command_name : not found
The shell restricts its search to the directories speciﬁed in the PATH variable. Sometimes when the shell is unable to ﬁnd a particular command, the command may reside in a directory that has not been speciﬁed in the PATH variable. When the shell is unable to ﬁnd a particular command, the user can type the absolute path name of the directory in which the command resides. For example:
If the shell is able to successfully execute the command when you specify the absolute path name of the directory, check the PATH variable to ensure that the directory exists in the search path. If the directory exists in the search path, check that it has been named correctly. For example:
$ echo $PATH /usr/dt/bin:/usr/openwin/bin:/usr/bin:/usr/ucb $
Extending the PATH Variable
To extend the PATH variable to include the home directory of the user, perform the following commands.
$ echo $PATH /usr/dt/bin:/usr/openwin/bin:/usr/bin:/usr/ucb $ $ PATH=$PATH:~ $ $ echo $PATH /usr/dt/bin:/usr/openwin/bin:/usr/bin:/usr/ucb:/export/home/user1
The PATH variable passes the value automatically to the sub-shells.
Using the Quoting Characters
Quoting is a process that instructs the shell to mask, or ignore, the special meaning of metacharacters. The quoting characters are single forward quotation marks (’’), double quotation marks (""), backslash (\), and parentheses ($( command )). Quotation marks around a string of metacharacters prevent the shell from interpreting the special meaning of the metacharacters. There are two types of quotation marks that mask the special meaning of metacharacters: single forward quotation marks (’’) and double quotation marks ("").
Single forward quotation marks instruct the shell to ignore all enclosed metacharacters. Double quotation marks instruct the shell to ignore all enclosed metacharacters, except for the following three characters:
1. The single backward quotation marks (‘) – Single backward quotation marks instruct the shell to execute and display the output for a UNIX system command. 2. The backslash (\) – A backslash (\) character in front of a metacharacter prevents the shell from interpreting the next character as a metacharacter. 3. The dollar sign ($) – You can also use parentheses () to perform command substitution. Parentheses ($( command )) instructs the shell to execute and display the output for the command enclosed within. The output of the nested command, command2, is substituted when the command1 command is executed. The syntax for this is:
To ignore the special meaning of the dollar sign metacharacter, perform the following command:
$ echo ’$SHELL’ $SHELL
NOTE: The echo utility writes arguments to the standard output.
To interpret the special meaning of the dollar sign metacharacter, perform the following command:
$ echo "$SHELL" /bin/ksh
To ignore the special meaning of the dollar sign metacharacter when you use double quotes, perform the following command:
$ echo "\$SHELL" $SHELL
To display the output for the date command, perform the following command:
$ echo “Today’s date is ‘date‘” Today’s date is Wed Dec 15 15:52:56 GMT 2004 $
To execute the pwd command using parentheses, perform the following command:
$ echo “The user is currently in the $(pwd) directory.” The user is currently in the /export/home/user1 directory.