0% found this document useful (0 votes)
40 views12 pages

Fundamentals of Unix Shell Programming 1

The document discusses fundamentals of Unix shell programming including variables, types of variables, declaring variables, special variables, basic operators, decision making using if/else and case statements.

Uploaded by

4rgx7fmvg9
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)
40 views12 pages

Fundamentals of Unix Shell Programming 1

The document discusses fundamentals of Unix shell programming including variables, types of variables, declaring variables, special variables, basic operators, decision making using if/else and case statements.

Uploaded by

4rgx7fmvg9
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/ 12

FUNDAMENTALS OF UNIX

SHELL PROGRAMMING

by Olufemi Olaewe
Variable

A variable is defined as a character string to which a value can be assigned.


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

The name of a variable can contain only letters (a to z or A to Z), numbers (0 to9) or the
underscore character (_) and must begin with a letter or underscore.
By convention, Unix Shell variables would have their names in UPPERCASE.
E.g. valid names: GLO, TOKENA, VAR_1, VAR_2

invalid names: 2_VAR, -VARIABLE, VAR1-VAR2, VAR_A!

!,*, and - have special meaning to the shell and cannot be used as part of variable names
Type of Variable

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 command prompt.

Environment Variables: An environment variable is a variable that is available to any child


process of the shell.

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.
Declari g/Defi i g variable

A variable definition takes the following format:


VARIABLE_NAME=variable_value

The value of a variable can be accessed by preceding its name with the $ sign

Example of environment variables

Running the script below

Will produce output


Special Variable

These are variables reserved by the system for specific functions

nb:

$* and $@ both will act the same unless they are enclosed in double quotes, "".

Both the parameters specify the command-line arguments. However, the "$*" special parameter
takes the entire list as one argument with spaces between and the "$@" special parameter
takes the entire list and separates it into separate arguments.
Ba ic Operator

Operators common to the default (Bourne) shell include:

Arithmetic Operators.

Relational Operators.
Boolean Operators.
String Operators.

File Test Operators.


Deci io Maki g

The need may arise for decision making

Unix Shell supports conditional statements, which are used to perform differentactions based
on different conditions.

The two main decision making statements are:

The if...else statements


The case...esac statement

The if...else statements include:

if…fi,
if...else...fi, and

if...elif...else...fi
if…fi tate e t

The if...fi statement is the fundamental control statement in shell programming

It allows the shell to make decisions and execute statements conditionally.

expression is evaluated and if the resulting value is true, the given statement(s) are
executed.
Also, if expression is a shell command it would be assumed true if it return 0 after execution
and if it is a Boolean expression, it would be true if it returns true. 5

You might also like