Operating System Lab 7
Operating System Lab 7
2021
LAB 07
LINUX OPERATING SYSTEM
Shell Scripting (Continue)
Variables in Shell Scripting:
A variable is nothing more than a pointer to the actual data. The shell
enables you to create, assign, and delete variables.
In simple words you can say that, a variable is nothing but a
placeholder for storing some value or data.
Referencing the value of the variable is called variable referencing.
Next, we will learn about using variables in shell scripting.
Variable Names
The name of a variable can contain only letters (a-z or A to Z), numbers
(0 to 9) or the underscore character (_).
The following examples are valid variable names:
i. _Variable
ii. Variable_1
iii. Var_A
The following examples are invalid variable names:
iv. 2_VAR
v. -VARIABLE
vi. VAR1-VAR2
vii. VAR_A!
The reason you cannot use other characters (such as !, *, or -) is that these
characters have a special meaning for the shell.
How to assign value to a variable?
The syntax for using a variable in shell scripting is simple:
var_name=value
For example:
Output:
Note: The above example defines the variable var_name and assigns the
value "hello" to it.
Remember: Never give space before and after the equal-to (=) sign or
else the script will throw an error during execution.
Output:
The variable var_name is assigned the value hello.
Next, echo command prints the value of the variable var_name.
Note: To access the value stored in a variable, prefix its name with
the dollar sign ($).
We use the ‘$’ sign with the variable name. Whenever you want to
use the value of the variable always use the $ sign with the variable
name.
If the $ sign is not used with the variable name then the script will
assume that you want to work on the variable name and not its value.
Now, we will modify the above script as below and observe the difference.
Output:
Example 2:
Now we considered another example. Here the scenario is, take a variable
and assign a value to it. Then use the echo command and display the value
assigned to the variable. Assign a new value to the same variable. Print
the newly assigned value after delay of two seconds.
In above scenario first we assign the value “anees asghar” to the variable
“Name”, and after some time we modified the same variable with the
value “Muhammad Ali”. In this example we observed that we can change
the value of variable.
Output:
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.
Output:
In above example, it is clearly seen that for first variable it prints the value
accordingly but when we try to change the value of variable. It shows an
error “read only variable” which means that, you can’t change the value
of read only variable. And it shows the old value of variable again when
we executed the echo command.
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.
Output:
The above example does not print anything. Because the above command
unsets the value of a defined variable.
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:
Variables visible only within a code block or function. For example the
variables used in the above scripts are local variables.
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.
Environment Variables affect the behavior of the shell and user
interface. For example, HOME, SHELL, PATH etc.
Environmental variables are generally declared in Uppercase.
Environment variables are predefined variables.
Output:
Lab Tasks
Write an example of read only variable.
Write a shell script to create an empty file. But the name of the file
should be passed by the user. It also, display a confirmation
message.
Write a shell script to create 3 directories. But the name of the file
should be passed by the user.
Write a script to change the name of a file. Ask from the user both
the old name as well as the new file name.