24.07.2018 Views

Bash-Beginners-Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-a Variable is an array.<br />

-f Use function names only.<br />

-i<br />

The variable is to be treated as an integer; arithmetic evaluation is performed when the variable is<br />

assigned a value (see Section 3.4.6).<br />

-p Display the attributes and values of each variable. When -p is used, additional options are ignored.<br />

-r<br />

Make variables read-only. These variables cannot then be assigned values by subsequent assignment<br />

statements, nor can they be unset.<br />

-t Give each variable the trace attribute.<br />

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

-x Mark each variable for export to subsequent commands via the environment.<br />

Using + instead of - turns off the attribute instead. When used in a function, declare creates local variables.<br />

The following example shows how assignment of a type to a variable influences the value.<br />

[bob in ~] declare -i VARIABLE=12<br />

[bob in ~] VARIABLE=string<br />

[bob in ~] echo $VARIABLE<br />

0<br />

[bob in ~] declare -p VARIABLE<br />

declare -i VARIABLE="0"<br />

Note that <strong>Bash</strong> has an option to declare a numeric value, but none for declaring string values. This is because,<br />

by default, if no specifications are given, a variable can hold any type of data:<br />

[bob in ~] OTHERVAR=blah<br />

[bob in ~] declare -p OTHERVAR<br />

declare -- OTHERVAR="blah"<br />

As soon as you restrict assignment of values to a variable, it can only hold that type of data. Possible<br />

restrictions are either integer, constant or array.<br />

See the <strong>Bash</strong> info pages for information on return status.<br />

10.1.3. Constants<br />

In <strong>Bash</strong>, constants are created by making a variable read-only. The readonly built-in marks each specified<br />

variable as unchangeable. The syntax is:<br />

readonly OPTION VARIABLE(s)<br />

The values of these variables can then no longer be changed by subsequent assignment. If the -f option is<br />

given, each variable refers to a shell function; see Chapter 11. If -a is specified, each variable refers to an<br />

array of variables. If no arguments are given, or if -p is supplied, a list of all read-only variables is displayed.<br />

Using the -p option, the output can be reused as input.<br />

The return status is zero, unless an invalid option was specified, one of the variables or functions does not<br />

exist, or -f was supplied for a variable name instead of for a function name.<br />

[bob in ~] readonly TUX=penguinpower<br />

Chapter 10. More on variables 122

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!