Basic Shell Programming Part 1
Basic Shell Programming Part 1
An Operating is made of many components, but its two prime components are -
Kernel
Shell
A Kernel is at the nucleus of a computer. It makes the communication between the hardware and software
possible. While the Kernel is the innermost part of an operating system, a shell is the outermost one.
A shell in a Linux operating system takes input from you in the form of commands, processes it, and then gives
an output. It is the interface through which a user works on the programs, commands, and scripts. A shell is
accessed by a terminal which runs it.
The Shell wraps around the delicate interior of an Operating system protecting it from accidental damage.
Hence the name Shell.
Types of Shell
There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:
Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repetitive
sequences of commands into a single and simple script, which can be stored and executed anytime. This reduces
the effort required by the end user.
1. Create a file using a vi editor(or any other editor). Name script file with extension .sh
2. Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh
6. "#!" is an operator called shebang which directs the script to the interpreter location. So, if we use"#! /bin/sh"
the script gets directed to the bourne-shell.
7. Let's create a small script -
8. #!/bin/sh
9. ls
10. Let's see the steps to create it -
11.
12. Command 'ls' is executed when we execute the scrip sample.sh file.
Variables store data in the form of characters and numbers. Similarly, Shell variables are used to store information
and they can by the shell only.
For example, the following creates a shell variable and then prints it:
variable ="Hello"
echo $variable
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
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 (
_).
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 −
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 −
Live Demo
#!/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 −
Live Demo
#!/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
The above example does not print anything. You cannot use the unset command to unset variables that are
marked readonly.
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.
For example, the $ character represents the process ID number, or PID, of the current shell −
$echo $$
The following table shows a number of special variables that you can use in your shell scripts −
1
$0
2 $n
3 $#
4 $*
All the arguments are double quoted. If a script receives two arguments,
$* is equivalent to $1 $2.
5 $@
All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.
6 $?
7 $$
The process number of the current shell. For shell scripts, this is the
process ID under which they are executing.
8 $!