Working with Variables and Parameters

Variables

Variables are defined simply by assigning a value to them.

NAME=abc

Note: There must be NO space between the variable name and the value you are assigning to it. For e.g. NAME = abc is incorrect

To access the value of the variable, precede the variable name with the $ sign. See example below :

echo $NAME

Output: abc

Note: I didn't have to surround my string abc with quotes. Also note that I used uppercase to define the variable. This is just a matter of preference. It is easier to identify variables if you stick to a particular convention or style, like using uppercase. Remember though that variables are case sensitive so NAME is not the same as name.

So we can assign another value to variable name and it will work.

name=cde

echo $name

echo $NAME

Output:

cde

abc

When assigning values to a variable NEVER put a space between the variable and the equal to sign and the value to assign to the variable or you will get an error saying -bash : <variable>: command not found.

$ name = cde

-bash: name: command not found

It is a good practice to keep all the variable declarations together, in one place, usually at the very beginning of the script. Other good practices are to keep the variable declarations in a separate file and then to include that file in the main script file.

Defining variables

There are three ways to define variables:

  • Static: VARNAME=value

  • As an argument to a script, handled using $1, $2 etc. within the script

  • Interactively, using read

To request the current value of a variable use the variable name preceeded by the $ sign.

e.g. echo $NAME

Scope of variables in shells and subshells

A variable is effective only in the shell where it was defined. In order to make a variable available to the sub-shells, use the export command.

Note: There is no way to make variables available in parent shells.

EXPORT command

A variable can be either a local variable or an environment variable. They both work the same way; the only difference lies in what happens when the script runs another program. Environment variables are passed to sub processes. Local variables are not. By default, variables are local. To turn a local variable into an environment variable, use the EXPORT command. So if you want the variable to be accessible to child scripts that are launched by the current shell script, use the EXPORT command. EXPORT marks an environment variable to be exported to child-processes, so that the child inherits them.

Demo :

Create a shell script with the following contents:

$ vi myapp.sh

echo "My name is: $NAME"

NAME=Bhatia

echo "My name is: $NAME"

Next execute the following commands :

$ NAME=Rakhi

$ bash myapp.sh

You will get the following output:

My name is:

My name is: Bhatia

Note that the first time we got no result for $NAME even though we assigned the value Rakhi to the NAME variable. That is because the NAME variable was not available to the myapp.sh script when it ran.

Now, let's export the NAME variable and see what happens

$ export NAME=Rakhi

$ bash myapp.sh

Now the output is:

My name is: Rakhi

My name is: Bhatia

Using variables with READ

The read command is used to read input from the user. If an argument is provided to read, the argument will be treated as a variable, and whatever the use enters as input, will be assigned to the variable. e.g. read name

Below is a sample script:

echo Enter the Process ID that you want to kill ?

read TOKILL

kill $TOKILL

In the above script, in line #2, the script waits for user input. Once the user inputs something, it gets stored in the TOKILL variable which is subsequently used with the kill command to kill a particular process.

Multiple arguments can also be provided to enter multiple variables e.g.

  `$read FNAME LNAME`

  `echo "New name is $FNAME ----- $LNAME`

Now, when prompted, if the user enters the words John Doe (with the 2 words separated by a space) followed by ENTER, the output will be :

New name is John ------ Doe

The first word "John" was stored in the FNAME variable while the second word Doe was stored int he LNAME variable

If the user had entered only a single word followed by ENTER e.g. John, then only FNAME would store the value John, while LNAME would be blank/empty string. The output in this case would have been :

New name is John ------

If the user had entered more than two words, e.g. John Jane Doe, then FNAME would store the value John, while LNAME would store the value Jane Doe, and the output would be :

New name is John ------ Jane Doe

To store the entered data to an array :

You can use the switch -a to write all the words input into an array.

e.g. read -a myarr

would store all the words that are input into a variable of type array, with the name myarr

Notes:

  • read will stop the script until something is entered by the user. If no input is provided, the script will pause until the user pressed the Enter key.

To define variables at the start of a shell

Sometimes we may want some variables to be pre-defined every time the shell starts. We can do this by defining variables in special files.

/etc/profile/ is processed when opening a login shell

The login shell is the shell that is launched when a user first logs in to the server. Before the user sees the shell prompt, the contents of this file has been processed. All variables defined here are available in all subshells for that specific user

In addition to this, every user has its own user-specific version of their environment profile located in ~/.bash_profile

/etc/bashrc/ is processed when opening a subshell

Variables defined here are included in subshells from that point on.

User specific versions can be used in ~/.bashrc

results matching ""

    No results matching ""