1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5
>Typing variables: declare or
9
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
12
TITLE="Advanced Bash-Scripting Guide"
13
HREF="index.html"><LINK
15
TITLE="Another Look at Variables"
16
HREF="variables2.html"><LINK
18
TITLE="Another Look at Variables"
19
HREF="variables2.html"><LINK
21
TITLE="$RANDOM: generate random integer"
22
HREF="randomvar.html"><META
23
HTTP-EQUIV="Content-Style-Type"
24
CONTENT="text/css"><LINK
26
HREF="common/kde-common.css"
28
HTTP-EQUIV="Content-Type"
29
CONTENT="text/html; charset=iso-8859-1"><META
30
HTTP-EQUIV="Content-Language"
33
HREF="common/kde-localised.css"
35
TITLE="KDE-English"><LINK
37
HREF="common/kde-default.css"
39
TITLE="KDE-Default"></HEAD
47
STYLE="font-family: sans-serif;"
51
SUMMARY="Header navigation table"
60
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
68
HREF="variables2.html"
76
>Chapter 9. Another Look at Variables</TD
98
>9.2. Typing variables: <B
120
HREF="internal.html#BUILTINREF"
122
>, which are exact synonyms,
123
permit modifying the properties of variables. This is
124
a very weak form of the <I
135
available in certain programming languages. The
139
> command is specific to version
140
2 or later of Bash. The <I
144
also works in ksh scripts.</P
150
NAME="DECLAREOPSREF1"
152
>declare/typeset options</B
181
>This is the rough equivalent of the <B
188
> type qualifier. An attempt
189
to change the value of a <I
193
variable fails with an error message.</P
202
CLASS="PROGRAMLISTING"
203
> 1 declare -r var1=1
204
2 echo "var1 = $var1" # var1 = 1
206
4 (( var1++ )) # x.sh: line 4: var1: readonly variable</PRE
232
CLASS="PROGRAMLISTING"
233
> 1 declare -i number
234
2 # The script will treat subsequent occurrences of "number" as an integer.
237
5 echo "Number = $number" # Number = 3
240
8 echo "Number = $number" # Number = 0
241
9 # Tries to evaluate the string "three" as an integer.</PRE
247
>Certain arithmetic operations are permitted
248
for declared integer variables without the need
250
HREF="moreadv.html#EXPRREF"
253
HREF="internal.html#LETREF"
264
CLASS="PROGRAMLISTING"
266
2 echo "n = $n" # n = 6/3
270
6 echo "n = $n" # n = 2</PRE
299
CLASS="PROGRAMLISTING"
300
> 1 declare -a indices</PRE
313
HREF="arrays.html#ARRAYREF"
336
CLASS="PROGRAMLISTING"
337
> 1 declare -f</PRE
349
arguments in a script causes a listing of all the
351
HREF="functions.html#FUNCTIONREF"
354
defined in that script.</P
363
CLASS="PROGRAMLISTING"
364
> 1 declare -f function_name</PRE
373
>declare -f function_name</B
376
in a script lists just the function named.</P
383
HREF="internal.html#EXPORTREF"
395
CLASS="PROGRAMLISTING"
396
> 1 declare -x var3</PRE
402
>This declares a variable as available for exporting outside the
403
environment of the script itself.</P
416
CLASS="PROGRAMLISTING"
417
> 1 declare -x var3=373</PRE
427
assigning a value to a variable in the same statement
428
as setting its properties.</P
439
>Example 9-10. Using <I
442
> to type variables</B
451
CLASS="PROGRAMLISTING"
456
5 echo This is a function.
459
8 declare -f # Lists the function above.
463
12 declare -i var1 # var1 is an integer.
465
14 echo "var1 declared as $var1"
466
15 var1=var1+1 # Integer declaration eliminates the need for 'let'.
467
16 echo "var1 incremented by 1 is $var1."
468
17 # Attempt to change variable declared as integer.
469
18 echo "Attempting to change var1 to floating point value, 2367.1."
470
19 var1=2367.1 # Results in error message, with no change to variable.
471
20 echo "var1 is still $var1"
475
24 declare -r var2=13.36 # 'declare' permits setting a variable property
476
25 #+ and simultaneously assigning it a value.
477
26 echo "var2 declared as $var2" # Attempt to change readonly variable.
478
27 var2=13.37 # Generates error message, and exit from script.
480
29 echo "var2 is still $var2" # This line will not execute.
482
31 exit 0 # Script will not exit here.</PRE
499
SRC="common/caution.png"
511
HREF="subshells.html#SCOPEREF"
524
CLASS="PROGRAMLISTING"
536
12 bar # Prints bar.</PRE
551
CLASS="PROGRAMLISTING"
553
2 declare FOO="bar"
562
11 bar # Prints nothing.
565
14 # Thank you, Michael Iatrou, for pointing this out.</PRE
581
>9.2.1. Another use for <I
590
helpful in identifying variables, <A
591
HREF="othertypesv.html#ENVREF"
594
This can be especially useful with <A
595
HREF="arrays.html#ARRAYREF"
613
>declare | grep HOME</B
617
CLASS="COMPUTEROUTPUT"
637
>declare | grep zzy</B
641
CLASS="COMPUTEROUTPUT"
652
>Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</B
661
>echo ${Colors[@]}</B
665
CLASS="COMPUTEROUTPUT"
666
>purple reddish-orange light green</TT
674
>declare | grep Colors</B
678
CLASS="COMPUTEROUTPUT"
679
>Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</TT
703
HREF="declareref.html#AEN5557"
719
a variable means to classify it and restrict its properties.
720
For example, a variable <I
728
is no longer available for <A
729
HREF="refcards.html#STRINGOPSTAB"
741
CLASS="PROGRAMLISTING"
742
> 1 declare -i intvar
745
4 echo "$intvar" # 23
746
5 intvar=stringval
747
6 echo "$intvar" # 0</PRE
760
SUMMARY="Footer navigation table"
771
HREF="variables2.html"
789
HREF="randomvar.html"
799
>Another Look at Variables</TD
805
HREF="variables2.html"
813
>$RANDOM: generate random integer</TD
b'\\ No newline at end of file'