Using Shell Variables:
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.
A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables. And there is no concept of data-types in shell scripting, by default all the variables are treated as strings by the shell, but these behave differently from the normal string type variables viz… We can do arithmetic operations on these strings etc…
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 would have their names in UPPERCASE.
The following examples are valid variable names:
Following are the examples of invalid variable names:
The reason you cannot use other characters such as !,*, or – is that these characters have a special meaning for the shell.
Variables are defined as follows::
Above example defines the variable NAME and assigns it the value “bvl success”. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.
The shell enables you to store any value you want in a variable. For example:
VAR1=" bvl success"
To access the value stored in a variable, prefix its name with the dollar sign ($):
For example, following script would access the value of defined variable NAME and would print it on STDOUT:
This would produce following value:
The shell provides a way to mark variables as read-only by using the readonly command. After a variable is marked read-only, its value cannot be changed.
For example, following script would give error while trying to change the value of NAME:
This would produce following result:
/bin/sh: NAME: This variable is read only.
Unsetting or deleting a variable tells the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you would not be able to access stored value in the variable.
Following is the syntax to unset a defined variable using the unset command:
Above command would unset the value of a defined variable. Here is a simple example:
Above example would not print anything. You cannot use the unset command to unset variables that are marked readonly.
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 command prompt.
- Environment Variables: An environment variable is a variable that 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.
The non-alphanumeric characters are used in the names of special Unix variables. These variables are reserved for specific functions.
For example, the $ character represents the process ID number, or PID, of the current shell:
- $echo $$
Above command would write PID of the current shell:
The following table shows a number of special variables that you can use in your shell scripts:
|$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.|