Fundamentals of Unix Shell Programming 1
Fundamentals of Unix Shell Programming 1
SHELL PROGRAMMING
by Olufemi Olaewe
Variable
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
!,*, 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.
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
The value of a variable can be accessed by preceding its name with the $ sign
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
Arithmetic Operators.
Relational Operators.
Boolean Operators.
String Operators.
Unix Shell supports conditional statements, which are used to perform differentactions based
on different conditions.
if…fi,
if...else...fi, and
if...elif...else...fi
if…fi tate e t
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