[go: up one dir, main page]

0% found this document useful (0 votes)
7 views4 pages

UNIX-UNIT-III-TOPIC-6-SHELL Variables

The document explains shell variables in Unix, detailing their types: local, environment, and shell variables. It covers variable naming conventions, how to define, access, mark as read-only, and unset variables, along with important environment and special variables used in shell scripts. Additionally, it provides examples of valid and invalid variable names and their usage in scripts.

Uploaded by

Ramesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

UNIX-UNIT-III-TOPIC-6-SHELL Variables

The document explains shell variables in Unix, detailing their types: local, environment, and shell variables. It covers variable naming conventions, how to define, access, mark as read-only, and unset variables, along with important environment and special variables used in shell scripts. Additionally, it provides examples of valid and invalid variable names and their usage in scripts.

Uploaded by

Ramesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Shell variables in Unix

A variable is a character string to which we assign a value. The value


assigned could be a number, text, filename, device, or any other type of
data.

Variable Types

When a shell is running, three main types of variables are present −


Local Variables − A local variable is a variable that is present within
the current instance of the shell. It is not available to programs that
are started by the shell. They are set at the command prompt.
Environment Variables − An environment variable is available to any
child process of the shell. Some programs need environment variables
in order to function correctly. Usually, a shell script defines only
those environment variables that are needed by the programs that it
runs.
Shell Variables − A shell variable is a special variable that is set by
the shell and is required by the shell in order to function correctly.
Some of these variables are environment variables whereas others are
local variables.

Variable Names: The name of a variable can contain only letters (a to z or A to Z),
numbers ( 0 to 9) or the underscore character ( _). By convention, UNIX shell
variables will have their names in UPPERCASE.

The following examples are valid variable names −


_ALI
TOKEN_A
VAR_1
VAR_2
Following are the examples of invalid variable names −
2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!
The reason you cannot use other characters such as !, *, or - is that these
characters have a special meaning for the shell.

Defining Variables: Variables are defined as follows −

IT-Ramesh 9848353570 1
variable_name=variable_value
For example −
NAME="Zara Ali"

The above example defines the variable NAME and assigns the value "Zara Ali" to
it. Variables of this type are called scalar variables. A scalar variable can hold only
one value at a time.
Shell enables you to store any value you want in a variable. For example −
VAR1="Zara Ali"
VAR2=100
Accessing Values: To access the value stored in a variable, prefix its name with
the dollar sign ($)
For example, the following script will access the value of defined variable NAME
and print it on STDOUT
#!/bin/sh
NAME="Zara Ali"
echo $NAME
Read-only Variables: Shell provides a way to mark variables as read-only by using
the read-only command. After a variable is marked read-only, its value cannot be
changed.
For example, the following script generates an error while trying to change the
value of NAME
#!/bin/sh
NAME="Zara Ali"
readonly NAME
NAME="Qadiri"
Unsetting Variables: Unsetting or deleting a variable directs the shell to remove
the variable from the list of variables that it tracks. Once you unset a variable, you
cannot access the stored value in the variable.
Following is the syntax to unset a defined variable using the unset command −
unset variable_name
The above command unsets the value of a defined variable. Here is a simple
example that demonstrates how the command works
#!/bin/sh
NAME="Zara Ali"
unset NAME
echo $NAME

IT-Ramesh 9848353570 2
An important UNIX concept is the environment, which is defined by environment
variables.

UNIX Environment Variables

Following is the list of important environment variables. These variables are


set and accessed as mentioned below –

Variable Description

DISPLAY Contains the identifier for the display that X11 programs
should use by default.
HOME Indicates the home directory of the current user: the
default argument for the cd built-in command.
IFS Indicates the Internal Field Separator that is used by the
parser for word splitting after expansion.
PATH Indicates the search path for commands. It is a colon-
separated list of directories in which the shell looks for
commands.
PWD Indicates the current working directory as set by the cd
command.
RANDOM Generates a random integer between 0 and 32,767 each
time it is referenced.
TERM Refers to the display type.

TZ Refers to Time zone. It can take values like GMT, AST,


etc.
UID Expands to the numeric user ID of the current user,
initialized at the shell startup.

Unix Special Variables:

The following table shows a number of special variables that can be used in
your shell scripts −

IT-Ramesh 9848353570 3
Variable Description

$0 The filename of the current script.

$n These variables correspond to the arguments with which


a script was invoked.
Here n is a positive decimal number corresponding to
the position of an argument (the first argument is $1,
the second argument is $2, and so on).
$# The number of arguments supplied to a script.

$* All the arguments are double quoted. If a script receives


two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a
script receives two arguments, $@ is equivalent to $1
$2.
$? The exit status of the last command executed.

$$ The process number of the current shell. For shell


scripts, this is the process ID under which they are
executing.
$! The process number of the last background command.

IT-Ramesh 9848353570 4

You might also like