Debian Codegraph @

HTML/declareref.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Typing variables: declare or
6 typeset</TITLE
7 ><META
8 NAME="GENERATOR"
9 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
10 "><LINK
11 REL="HOME"
12 TITLE="Advanced Bash-Scripting Guide"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Another Look at Variables"
16 HREF="variables2.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Another Look at Variables"
19 HREF="variables2.html"><LINK
20 REL="NEXT"
21 TITLE="$RANDOM: generate random integer"
22 HREF="randomvar.html"><META
23 HTTP-EQUIV="Content-Style-Type"
24 CONTENT="text/css"><LINK
25 REL="stylesheet"
26 HREF="common/kde-common.css"
27 TYPE="text/css"><META
28 HTTP-EQUIV="Content-Type"
29 CONTENT="text/html; charset=iso-8859-1"><META
30 HTTP-EQUIV="Content-Language"
31 CONTENT="en"><LINK
32 REL="stylesheet"
33 HREF="common/kde-localised.css"
34 TYPE="text/css"
35 TITLE="KDE-English"><LINK
36 REL="stylesheet"
37 HREF="common/kde-default.css"
38 TYPE="text/css"
39 TITLE="KDE-Default"></HEAD
40 ><BODY
41 CLASS="SECT1"
42 BGCOLOR="#FFFFFF"
43 TEXT="#000000"
44 LINK="#AA0000"
45 VLINK="#AA0055"
46 ALINK="#AA0000"
47 STYLE="font-family: sans-serif;"
48 ><DIV
49 CLASS="NAVHEADER"
50 ><TABLE
51 SUMMARY="Header navigation table"
52 WIDTH="100%"
53 BORDER="0"
54 CELLPADDING="0"
55 CELLSPACING="0"
56 ><TR
57 ><TH
58 COLSPAN="3"
59 ALIGN="center"
60 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
61 ></TR
62 ><TR
63 ><TD
64 WIDTH="10%"
65 ALIGN="left"
66 VALIGN="bottom"
67 ><A
68 HREF="variables2.html"
69 ACCESSKEY="P"
70 >Prev</A
71 ></TD
72 ><TD
73 WIDTH="80%"
74 ALIGN="center"
75 VALIGN="bottom"
76 >Chapter 9. Another Look at Variables</TD
77 ><TD
78 WIDTH="10%"
79 ALIGN="right"
80 VALIGN="bottom"
81 ><A
82 HREF="randomvar.html"
83 ACCESSKEY="N"
84 >Next</A
85 ></TD
86 ></TR
87 ></TABLE
88 ><HR
89 ALIGN="LEFT"
90 WIDTH="100%"></DIV
91 ><DIV
92 CLASS="SECT1"
93 ><H1
94 CLASS="SECT1"
95 ><A
96 NAME="DECLAREREF"
97 ></A
98 >9.2. Typing variables: <B
99 CLASS="COMMAND"
100 >declare</B
101 > or
102 <B
103 CLASS="COMMAND"
104 >typeset</B
105 ></H1
106 ><P
107 ><A
108 NAME="DECLARE1REF"
109 ></A
110 ></P
111 ><P
112 >The <I
113 CLASS="FIRSTTERM"
114 >declare</I
115 > or
116 <I
117 CLASS="FIRSTTERM"
118 >typeset</I
119 > <A
120 HREF="internal.html#BUILTINREF"
121 >builtins</A
122 >, which are exact synonyms,
123 permit modifying the properties of variables. This is
124 a very weak form of the <I
125 CLASS="FIRSTTERM"
126 >typing</I
127 >
128
129 <A
130 NAME="AEN5685"
131 HREF="#FTN.AEN5685"
132 >[1]</A
133 >
134
135 available in certain programming languages. The
136 <I
137 CLASS="FIRSTTERM"
138 >declare</I
139 > command is specific to version
140 2 or later of Bash. The <I
141 CLASS="FIRSTTERM"
142 >typeset</I
143 > command
144 also works in ksh scripts.</P
145 ><DIV
146 CLASS="VARIABLELIST"
147 ><P
148 ><B
149 ><A
150 NAME="DECLAREOPSREF1"
151 ></A
152 >declare/typeset options</B
153 ></P
154 ><DL
155 ><DT
156 ><SPAN
157 CLASS="TOKEN"
158 >-r</SPAN
159 >
160 <TT
161 CLASS="REPLACEABLE"
162 ><I
163 >readonly</I
164 ></TT
165 ></DT
166 ><DD
167 ><P
168 >(<TT
169 CLASS="USERINPUT"
170 ><B
171 >declare -r var1</B
172 ></TT
173 > works the same as
174 <TT
175 CLASS="USERINPUT"
176 ><B
177 >readonly var1</B
178 ></TT
179 >)</P
180 ><P
181 >This is the rough equivalent of the <B
182 CLASS="COMMAND"
183 >C</B
184 >
185 <I
186 CLASS="FIRSTTERM"
187 >const</I
188 > type qualifier. An attempt
189 to change the value of a <I
190 CLASS="FIRSTTERM"
191 >readonly</I
192 >
193 variable fails with an error message.</P
194 ><P
195 ><TABLE
196 BORDER="0"
197 BGCOLOR="#E0E0E0"
198 WIDTH="90%"
199 ><TR
200 ><TD
201 ><PRE
202 CLASS="PROGRAMLISTING"
203 > 1&nbsp;declare -r var1=1
204 2&nbsp;echo "var1 = $var1" # var1 = 1
205 3&nbsp;
206 4&nbsp;(( var1++ )) # x.sh: line 4: var1: readonly variable</PRE
207 ></TD
208 ></TR
209 ></TABLE
210 >
211 </P
212 ></DD
213 ><DT
214 ><SPAN
215 CLASS="TOKEN"
216 >-i</SPAN
217 > <TT
218 CLASS="REPLACEABLE"
219 ><I
220 >integer</I
221 ></TT
222 ></DT
223 ><DD
224 ><P
225 ><TABLE
226 BORDER="0"
227 BGCOLOR="#E0E0E0"
228 WIDTH="90%"
229 ><TR
230 ><TD
231 ><PRE
232 CLASS="PROGRAMLISTING"
233 > 1&nbsp;declare -i number
234 2&nbsp;# The script will treat subsequent occurrences of "number" as an integer.
235 3&nbsp;
236 4&nbsp;number=3
237 5&nbsp;echo "Number = $number" # Number = 3
238 6&nbsp;
239 7&nbsp;number=three
240 8&nbsp;echo "Number = $number" # Number = 0
241 9&nbsp;# Tries to evaluate the string "three" as an integer.</PRE
242 ></TD
243 ></TR
244 ></TABLE
245 ></P
246 ><P
247 >Certain arithmetic operations are permitted
248 for declared integer variables without the need
249 for <A
250 HREF="moreadv.html#EXPRREF"
251 >expr</A
252 > or <A
253 HREF="internal.html#LETREF"
254 >let</A
255 >.</P
256 ><P
257 ><TABLE
258 BORDER="0"
259 BGCOLOR="#E0E0E0"
260 WIDTH="90%"
261 ><TR
262 ><TD
263 ><PRE
264 CLASS="PROGRAMLISTING"
265 > 1&nbsp;n=6/3
266 2&nbsp;echo "n = $n" # n = 6/3
267 3&nbsp;
268 4&nbsp;declare -i n
269 5&nbsp;n=6/3
270 6&nbsp;echo "n = $n" # n = 2</PRE
271 ></TD
272 ></TR
273 ></TABLE
274 ></P
275 ></DD
276 ><DT
277 ><A
278 NAME="ARRAYDECLARE"
279 ></A
280 ><SPAN
281 CLASS="TOKEN"
282 >-a</SPAN
283 >
284 <TT
285 CLASS="REPLACEABLE"
286 ><I
287 >array</I
288 ></TT
289 ></DT
290 ><DD
291 ><P
292 ><TABLE
293 BORDER="0"
294 BGCOLOR="#E0E0E0"
295 WIDTH="90%"
296 ><TR
297 ><TD
298 ><PRE
299 CLASS="PROGRAMLISTING"
300 > 1&nbsp;declare -a indices</PRE
301 ></TD
302 ></TR
303 ></TABLE
304 ></P
305 ><P
306 >The variable <TT
307 CLASS="PARAMETER"
308 ><I
309 >indices</I
310 ></TT
311 > will be treated as
312 an <A
313 HREF="arrays.html#ARRAYREF"
314 >array</A
315 >.</P
316 ></DD
317 ><DT
318 ><SPAN
319 CLASS="TOKEN"
320 >-f</SPAN
321 > <TT
322 CLASS="REPLACEABLE"
323 ><I
324 >function(s)</I
325 ></TT
326 ></DT
327 ><DD
328 ><P
329 ><TABLE
330 BORDER="0"
331 BGCOLOR="#E0E0E0"
332 WIDTH="90%"
333 ><TR
334 ><TD
335 ><PRE
336 CLASS="PROGRAMLISTING"
337 > 1&nbsp;declare -f</PRE
338 ></TD
339 ></TR
340 ></TABLE
341 ></P
342 ><P
343 >A <TT
344 CLASS="USERINPUT"
345 ><B
346 >declare -f</B
347 ></TT
348 > line with no
349 arguments in a script causes a listing of all the
350 <A
351 HREF="functions.html#FUNCTIONREF"
352 >functions</A
353 > previously
354 defined in that script.</P
355 ><P
356 ><TABLE
357 BORDER="0"
358 BGCOLOR="#E0E0E0"
359 WIDTH="90%"
360 ><TR
361 ><TD
362 ><PRE
363 CLASS="PROGRAMLISTING"
364 > 1&nbsp;declare -f function_name</PRE
365 ></TD
366 ></TR
367 ></TABLE
368 ></P
369 ><P
370 >A <TT
371 CLASS="USERINPUT"
372 ><B
373 >declare -f function_name</B
374 ></TT
375 >
376 in a script lists just the function named.</P
377 ></DD
378 ><DT
379 ><SPAN
380 CLASS="TOKEN"
381 >-x</SPAN
382 > <A
383 HREF="internal.html#EXPORTREF"
384 >export</A
385 ></DT
386 ><DD
387 ><P
388 ><TABLE
389 BORDER="0"
390 BGCOLOR="#E0E0E0"
391 WIDTH="90%"
392 ><TR
393 ><TD
394 ><PRE
395 CLASS="PROGRAMLISTING"
396 > 1&nbsp;declare -x var3</PRE
397 ></TD
398 ></TR
399 ></TABLE
400 ></P
401 ><P
402 >This declares a variable as available for exporting outside the
403 environment of the script itself.</P
404 ></DD
405 ><DT
406 >-x var=$value</DT
407 ><DD
408 ><P
409 ><TABLE
410 BORDER="0"
411 BGCOLOR="#E0E0E0"
412 WIDTH="90%"
413 ><TR
414 ><TD
415 ><PRE
416 CLASS="PROGRAMLISTING"
417 > 1&nbsp;declare -x var3=373</PRE
418 ></TD
419 ></TR
420 ></TABLE
421 ></P
422 ><P
423 >The <B
424 CLASS="COMMAND"
425 >declare</B
426 > command permits
427 assigning a value to a variable in the same statement
428 as setting its properties.</P
429 ></DD
430 ></DL
431 ></DIV
432 ><DIV
433 CLASS="EXAMPLE"
434 ><HR><A
435 NAME="EX20"
436 ></A
437 ><P
438 ><B
439 >Example 9-10. Using <I
440 CLASS="FIRSTTERM"
441 >declare</I
442 > to type variables</B
443 ></P
444 ><TABLE
445 BORDER="0"
446 BGCOLOR="#E0E0E0"
447 WIDTH="100%"
448 ><TR
449 ><TD
450 ><PRE
451 CLASS="PROGRAMLISTING"
452 > 1&nbsp;#!/bin/bash
453 2&nbsp;
454 3&nbsp;func1 ()
455 4&nbsp;{
456 5&nbsp; echo This is a function.
457 6&nbsp;}
458 7&nbsp;
459 8&nbsp;declare -f # Lists the function above.
460 9&nbsp;
461 10&nbsp;echo
462 11&nbsp;
463 12&nbsp;declare -i var1 # var1 is an integer.
464 13&nbsp;var1=2367
465 14&nbsp;echo "var1 declared as $var1"
466 15&nbsp;var1=var1+1 # Integer declaration eliminates the need for 'let'.
467 16&nbsp;echo "var1 incremented by 1 is $var1."
468 17&nbsp;# Attempt to change variable declared as integer.
469 18&nbsp;echo "Attempting to change var1 to floating point value, 2367.1."
470 19&nbsp;var1=2367.1 # Results in error message, with no change to variable.
471 20&nbsp;echo "var1 is still $var1"
472 21&nbsp;
473 22&nbsp;echo
474 23&nbsp;
475 24&nbsp;declare -r var2=13.36 # 'declare' permits setting a variable property
476 25&nbsp; #+ and simultaneously assigning it a value.
477 26&nbsp;echo "var2 declared as $var2" # Attempt to change readonly variable.
478 27&nbsp;var2=13.37 # Generates error message, and exit from script.
479 28&nbsp;
480 29&nbsp;echo "var2 is still $var2" # This line will not execute.
481 30&nbsp;
482 31&nbsp;exit 0 # Script will not exit here.</PRE
483 ></TD
484 ></TR
485 ></TABLE
486 ><HR></DIV
487 ><DIV
488 CLASS="CAUTION"
489 ><TABLE
490 CLASS="CAUTION"
491 WIDTH="100%"
492 BORDER="0"
493 ><TR
494 ><TD
495 WIDTH="25"
496 ALIGN="CENTER"
497 VALIGN="TOP"
498 ><IMG
499 SRC="common/caution.png"
500 HSPACE="5"
501 ALT="Caution"></TD
502 ><TD
503 ALIGN="LEFT"
504 VALIGN="TOP"
505 ><P
506 >Using the <I
507 CLASS="FIRSTTERM"
508 >declare</I
509 > builtin
510 restricts the <A
511 HREF="subshells.html#SCOPEREF"
512 >scope</A
513 >
514 of a variable.
515
516
517 <TABLE
518 BORDER="0"
519 BGCOLOR="#E0E0E0"
520 WIDTH="100%"
521 ><TR
522 ><TD
523 ><PRE
524 CLASS="PROGRAMLISTING"
525 > 1&nbsp;foo ()
526 2&nbsp;{
527 3&nbsp;FOO="bar"
528 4&nbsp;}
529 5&nbsp;
530 6&nbsp;bar ()
531 7&nbsp;{
532 8&nbsp;foo
533 9&nbsp;echo $FOO
534 10&nbsp;}
535 11&nbsp;
536 12&nbsp;bar # Prints bar.</PRE
537 ></TD
538 ></TR
539 ></TABLE
540 ></P
541 ><P
542 >However . . .
543
544 <TABLE
545 BORDER="0"
546 BGCOLOR="#E0E0E0"
547 WIDTH="100%"
548 ><TR
549 ><TD
550 ><PRE
551 CLASS="PROGRAMLISTING"
552 > 1&nbsp;foo (){
553 2&nbsp;declare FOO="bar"
554 3&nbsp;}
555 4&nbsp;
556 5&nbsp;bar ()
557 6&nbsp;{
558 7&nbsp;foo
559 8&nbsp;echo $FOO
560 9&nbsp;}
561 10&nbsp;
562 11&nbsp;bar # Prints nothing.
563 12&nbsp;
564 13&nbsp;
565 14&nbsp;# Thank you, Michael Iatrou, for pointing this out.</PRE
566 ></TD
567 ></TR
568 ></TABLE
569 ></P
570 ></TD
571 ></TR
572 ></TABLE
573 ></DIV
574 ><DIV
575 CLASS="SECT2"
576 ><H2
577 CLASS="SECT2"
578 ><A
579 NAME="DECLARE2X"
580 ></A
581 >9.2.1. Another use for <I
582 CLASS="FIRSTTERM"
583 >declare</I
584 ></H2
585 ><P
586 >The <I
587 CLASS="FIRSTTERM"
588 >declare</I
589 > command can be
590 helpful in identifying variables, <A
591 HREF="othertypesv.html#ENVREF"
592 >environmental</A
593 > or otherwise.
594 This can be especially useful with <A
595 HREF="arrays.html#ARRAYREF"
596 >arrays</A
597 >.</P
598 ><P
599 > <TABLE
600 BORDER="0"
601 BGCOLOR="#E0E0E0"
602 WIDTH="100%"
603 ><TR
604 ><TD
605 ><PRE
606 CLASS="SCREEN"
607 > <TT
608 CLASS="PROMPT"
609 >bash$ </TT
610 ><TT
611 CLASS="USERINPUT"
612 ><B
613 >declare | grep HOME</B
614 ></TT
615 >
616 <TT
617 CLASS="COMPUTEROUTPUT"
618 >HOME=/home/bozo</TT
619 >
620
621
622 <TT
623 CLASS="PROMPT"
624 >bash$ </TT
625 ><TT
626 CLASS="USERINPUT"
627 ><B
628 >zzy=68</B
629 ></TT
630 >
631 <TT
632 CLASS="PROMPT"
633 >bash$ </TT
634 ><TT
635 CLASS="USERINPUT"
636 ><B
637 >declare | grep zzy</B
638 ></TT
639 >
640 <TT
641 CLASS="COMPUTEROUTPUT"
642 >zzy=68</TT
643 >
644
645
646 <TT
647 CLASS="PROMPT"
648 >bash$ </TT
649 ><TT
650 CLASS="USERINPUT"
651 ><B
652 >Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</B
653 ></TT
654 >
655 <TT
656 CLASS="PROMPT"
657 >bash$ </TT
658 ><TT
659 CLASS="USERINPUT"
660 ><B
661 >echo ${Colors[@]}</B
662 ></TT
663 >
664 <TT
665 CLASS="COMPUTEROUTPUT"
666 >purple reddish-orange light green</TT
667 >
668 <TT
669 CLASS="PROMPT"
670 >bash$ </TT
671 ><TT
672 CLASS="USERINPUT"
673 ><B
674 >declare | grep Colors</B
675 ></TT
676 >
677 <TT
678 CLASS="COMPUTEROUTPUT"
679 >Colors=([0]="purple" [1]="reddish-orange" [2]="light green")</TT
680 >
681 </PRE
682 ></TD
683 ></TR
684 ></TABLE
685 >
686 </P
687 ></DIV
688 ></DIV
689 ><H3
690 CLASS="FOOTNOTES"
691 >Notes</H3
692 ><TABLE
693 BORDER="0"
694 CLASS="FOOTNOTES"
695 WIDTH="100%"
696 ><TR
697 ><TD
698 ALIGN="LEFT"
699 VALIGN="TOP"
700 WIDTH="5%"
701 ><A
702 NAME="FTN.AEN5685"
703 HREF="declareref.html#AEN5685"
704 >[1]</A
705 ></TD
706 ><TD
707 ALIGN="LEFT"
708 VALIGN="TOP"
709 WIDTH="95%"
710 ><P
711 ><A
712 NAME="TYPINGREF"
713 ></A
714 >In this context,
715 <I
716 CLASS="FIRSTTERM"
717 >typing</I
718 >
719 a variable means to classify it and restrict its properties.
720 For example, a variable <I
721 CLASS="FIRSTTERM"
722 >declared</I
723 >
724 or <I
725 CLASS="FIRSTTERM"
726 >typed</I
727 > as an integer
728 is no longer available for <A
729 HREF="refcards.html#STRINGOPSTAB"
730 >string
731 operations</A
732 >.</P
733 ><P
734 ><TABLE
735 BORDER="0"
736 BGCOLOR="#E0E0E0"
737 WIDTH="100%"
738 ><TR
739 ><TD
740 ><PRE
741 CLASS="PROGRAMLISTING"
742 > 1&nbsp;declare -i intvar
743 2&nbsp;
744 3&nbsp;intvar=23
745 4&nbsp;echo "$intvar" # 23
746 5&nbsp;intvar=stringval
747 6&nbsp;echo "$intvar" # 0</PRE
748 ></TD
749 ></TR
750 ></TABLE
751 ></P
752 ></TD
753 ></TR
754 ></TABLE
755 ><DIV
756 CLASS="NAVFOOTER"
757 ><HR
758 ALIGN="LEFT"
759 WIDTH="100%"><TABLE
760 SUMMARY="Footer navigation table"
761 WIDTH="100%"
762 BORDER="0"
763 CELLPADDING="0"
764 CELLSPACING="0"
765 ><TR
766 ><TD
767 WIDTH="33%"
768 ALIGN="left"
769 VALIGN="top"
770 ><A
771 HREF="variables2.html"
772 ACCESSKEY="P"
773 >Prev</A
774 ></TD
775 ><TD
776 WIDTH="34%"
777 ALIGN="center"
778 VALIGN="top"
779 ><A
780 HREF="index.html"
781 ACCESSKEY="H"
782 >Home</A
783 ></TD
784 ><TD
785 WIDTH="33%"
786 ALIGN="right"
787 VALIGN="top"
788 ><A
789 HREF="randomvar.html"
790 ACCESSKEY="N"
791 >Next</A
792 ></TD
793 ></TR
794 ><TR
795 ><TD
796 WIDTH="33%"
797 ALIGN="left"
798 VALIGN="top"
799 >Another Look at Variables</TD
800 ><TD
801 WIDTH="34%"
802 ALIGN="center"
803 VALIGN="top"
804 ><A
805 HREF="variables2.html"
806 ACCESSKEY="U"
807 >Up</A
808 ></TD
809 ><TD
810 WIDTH="33%"
811 ALIGN="right"
812 VALIGN="top"
813 >$RANDOM: generate random integer</TD
814 ></TR
815 ></TABLE
816 ></DIV
817 ></BODY
818 ></HTML
819 >