What and how to use UNIX Variables with examples

Posted at August 22, 2016 at 10:31 am by Jithin

When you run a program, variables act as a way of passing information from shell to programs.  At first, a program will look at the particular variables in their environment and if it is found it will use the value stored. Some of the variables are set by the system, others by our self, others by the shell, or any program that loads another programs.

Standard UNIX variables are classified into two.

1) Environment variables

2) Shell variables

A specific environment is needed to run every UNIX process. When you log into a certain login file, table of environment variables is executed. An environment consists of table of environment variables, each with an assigned value. The tables hold the environment variables for the process. The shell variables also maintain the internal variables. Shell work in a particular way because of these variables. Shell variables are defined, as they are not available to the parent or child shell. Shell variables names are commonly written in lower case in the C shell family and upper case in the Bourne shell family.


Environment variables

An environment variable is available to any child process of the shell. Some programs functions work correctly because of environment variables. Usually a shell script defines only those environment variables that are needed by the programs that it runs.

Some environment variables are set by default.

For eg,






“setenv” command is used to set environment variables.

“printenv” or “env” command is used to display.

“unsetenv” is used for unset.

The format of the commands:

% setenv [NAME value]

% unsetenv NAME

% printenv | less


Shell variables

A shell variable is set by the shell is a special variables and the functions are enabled by the shell. Some of these variables are environment variables and others are local variables. Four important shell variables are automatically initialized to contain the same values as the corresponding environment variables. These are:

1) user

2) home

3) term

4) path

If any of these are changed, the corresponding environment variables will also be changed.

More examples of shell variables are:

1) history

2) cwd

3) prompt

“set” command is used to set and display the shell variables.

% set name=value

“unset” command is used to unset or delete shell variables.

% unset name

The following are some of the special variables is used in shell script.

1) $0 -The file name of the current script.

2) $n – These variables correspond to the arguments with which a script was invoked.

3) $# – The number of arguments supplied to a script.

4) $* – All the arguments are double quoted.

5) $@ – All the arguments are individually double quoted.

6) $? – The exit status of the last command executed.

7) $$ – The process number of the current shell.

8) $! – The process number of the last background command.


Using and setting variables

When you are login to a UNIX host, the system looks the home directory for beginning files. To set up the working environment refer these file.

.login and .cshrc are the C and TC shells.

To set conditions to whole session and perform actions by using .login

To set conditions and perform actions to the shell and invocation by using .cshrc

.login file are used to set environment variables and .cshrc is used to set shell variables.


Setting shell variables in the .cshrc file

For example, the history list to change the number of shell commands are saved, set the shell variable history. By default, it set as 100.

We can change the variable history by,

% set history = 200

Check this has worked by typing

% echo $history

Add “set” command to the .cshrc files to permanently set the value of history.

First open the .cshrc file in a text editor. Here I choose the editor to use is nedit.

% nedit ~/.cshrc

Add the following line after the list of other commands.

set history = 200

Save the file and force the shell to reread its .cshrc file buy using the shell source command.

% source .cshrc

Check this by typing the command.

% echo $history


If you need any further assistance please reach our support department.



0.00 avg. rating (0% score) - 0 votes

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply