6.2. Typed Variables

 < Day Day Up > 

So far we've seen how bash variables can be assigned textual values. Variables can also have other attributes, including being read only and being of type integer.

You can set variable attributes with the declare built-in. [6] Table 6-1 summarizes the available options with declare.[7] A - turns the option on, while + turns it off.

[6] The typeset built-in is synonymous with declare but is considered obsolete.

[7] The -a and -F options are not available in bash prior to version 2.0.

Table 6-1. Declare options

Option

Meaning

-a

The variables are treated as arrays

-f

Use function names only

-F

Display function names without definitions

-i

The variables are treated as integers

-r

Makes the variables read-only

-x

Marks the variables for export via the environment


Typing declare on its own displays the values of all variables in the environment. The -f option limits this display to the function names and definitions currently in the environment. -F limits it further by displaying only the function names.

The -a option declares arrays a variable type that we haven't seen yet, but will be discussed shortly.

The -i option is used to create an integer variable, one that holds numeric values and can be used in and modified by arithmetic operations. Consider this example:

$ val1=12 val2=5 $ result1=val*val2 $ echo $result1 val1*val2 $ $ declare -i val3=12 val4=5 $ declare -i result2 $ result2=val3*val4 $ echo $result2 60

In the first example, the variables are ordinary shell variables and the result is just the string "val1*val2". In the second example, all of the variables have been declared as type integer. The variable result contains the result of the arithmetic computation twelve multiplied by five. Actually, we didn't need to declare val3 and val4 as type integer. Anything being assigned to result2 is interpreted as an arithmetic statement and evaluation is attempted.

The -x option to declare operates in the same way as the export built-in that we saw in Chapter 3. It allows the listed variables to be exported outside the current shell environment.

The -r option creates a read-only variable, one that cannot have its value changed by subsequent assignment statements and cannot be unset.

A related built-in is readonly name ... which operates in exactly the same way as declare -r. readonly has three options: -f, which makes readonly interpret the name arguments as function names rather than variable names, -p, which makes the built-in print a list of all read-only names, and -a, which interprets the name arguments as arrays.

Lastly, variables declared in a function are local to that function, just like using local to declare them.

     < Day Day Up > 


    Learning the bash Shell
    Learning the bash Shell: Unix Shell Programming (In a Nutshell (OReilly))
    ISBN: 0596009658
    EAN: 2147483647
    Year: 2005
    Pages: 139

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net