Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 10. More on variables<br />
In this chapter, we will discuss the advanced use of variables and arguments. Upon<br />
completion, you will be able to:<br />
♦ Declare and use an array of variables<br />
♦ Specify the sort of variable you want to use<br />
♦ Make variables read-only<br />
♦ Use set to assign a value to a variable<br />
10.1. Types of variables<br />
10.1.1. General assignment of values<br />
As we already saw, <strong>Bash</strong> understands many different kinds of variables or parameters. Thus far, we haven't<br />
bothered much with what kind of variables we assigned, so our variables could hold any value that we<br />
assigned to them. A simple command line example demonstrates this:<br />
[bob in ~] VARIABLE=12<br />
[bob in ~] echo $VARIABLE<br />
12<br />
[bob in ~] VARIABLE=string<br />
[bob in ~] echo $VARIABLE<br />
string<br />
There are cases when you want to avoid this kind of behavior, for instance when handling telephone and other<br />
numbers. Apart from integers and variables, you may also want to specify a variable that is a constant. This is<br />
often done at the beginning of a script, when the value of the constant is declared. After that, there are only<br />
references to the constant variable name, so that when the constant needs to be changed, it only has to be done<br />
once. A variable may also be a series of variables of any type, a so-called array of variables (VAR0VAR1,<br />
VAR2, ... VARN).<br />
10.1.2. Using the declare built-in<br />
Using a declare statement, we can limit the value assignment to variables.<br />
The syntax for declare is the following:<br />
declare OPTION(s) VARIABLE=value<br />
The following options are used to determine the type of data the variable can hold and to assign it attributes:<br />
Table 10-1. Options to the declare built-in<br />
Option Meaning<br />
Chapter 10. More on variables 121