Working with Variables

To create a good, working shell script, using variables is an important feature. In the previous section, you learned how to use variables to store the arguments that are entered when activating a script. You can define variables in other ways as well. In the following sections, you'll explore a few more of the possibilities when working with variables.

A variable is a reserved amount of memory with a name that you can use to store information that a script uses more than once. Using variables also makes it easier to be flexible with values that can change. A common way of defining a variable is to specify the name of the variable, followed by an = and its value; for example, today=Thursday will grant the value Thursday to today. Later in a script, it is easy to refer to the value of this variable; for example, the command echo $today will give the current value of the variable. The advantage of working with variables this way is that when the value needs to change (for example, because it is Friday), you can change this new value in one location in the script only.

Was this article helpful?

0 0

Post a comment