9.4. Typing variables: declare or typeset

The declare or typeset builtins (they are exact synonyms) permit restricting the properties of variables. This is a very weak form of the typing available in certain programming languages. The declare command is specific to version 2 or later of Bash. The typeset command also works in ksh scripts.

declare/typeset options

-r readonly

   1 declare -r var1

(declare -r var1 works the same as readonly var1)

This is the rough equivalent of the C const type qualifier. An attempt to change the value of a readonly variable fails with an error message.

-i integer

   1 declare -i number
   2 # The script will treat subsequent occurrences of "number" as an integer.		
   3 
   4 number=3
   5 echo "Number = $number"     # Number = 3
   6 
   7 number=three
   8 echo "Number = $number"     # Number = 0
   9 # Tries to evaluate the string "three" as an integer.

Certain arithmetic operations are permitted for declared integer variables without the need for expr or let.

   1 n=6/3
   2 echo "n = $n"       # n = 6/3
   3 
   4 declare -i n
   5 n=6/3
   6 echo "n = $n"       # n = 2

-a array

   1 declare -a indices

The variable indices will be treated as an array.

-f functions

   1 declare -f

A declare -f line with no arguments in a script causes a listing of all the functions previously defined in that script.

   1 declare -f function_name

A declare -f function_name in a script lists just the function named.

-x export

   1 declare -x var3

This declares a variable as available for exporting outside the environment of the script itself.

-x var=$value

   1 declare -x var3=373

The declare command permits assigning a value to a variable in the same statement as setting its properties.


Example 9-22. Using declare to type variables

   1 #!/bin/bash
   2 
   3 func1 ()
   4 {
   5 echo This is a function.
   6 }
   7 
   8 declare -f        # Lists the function above.
   9 
  10 echo
  11 
  12 declare -i var1   # var1 is an integer.
  13 var1=2367
  14 echo "var1 declared as $var1"
  15 var1=var1+1       # Integer declaration eliminates the need for 'let'.
  16 echo "var1 incremented by 1 is $var1."
  17 # Attempt to change variable declared as integer.
  18 echo "Attempting to change var1 to floating point value, 2367.1."
  19 var1=2367.1       # Results in error message, with no change to variable.
  20 echo "var1 is still $var1"
  21 
  22 echo
  23 
  24 declare -r var2=13.36         # 'declare' permits setting a variable property
  25                               #+ and simultaneously assigning it a value.
  26 echo "var2 declared as $var2" # Attempt to change readonly variable.
  27 var2=13.37                    # Generates error message, and exit from script.
  28 
  29 echo "var2 is still $var2"    # This line will not execute.
  30 
  31 exit 0                        # Script will not exit here.

Caution

Using the declare builtin restricts the scope of a variable.
   1 foo ()
   2 {
   3 FOO="bar"
   4 }
   5 
   6 bar ()
   7 {
   8 foo
   9 echo $FOO
  10 }
  11 
  12 bar   # Prints bar.

However . . .
   1 foo (){
   2 declare FOO="bar"
   3 }
   4 
   5 bar ()
   6 {
   7 foo
   8 echo $FOO
   9 }
  10 
  11 bar  # Prints nothing.
  12 
  13 
  14 # Thank you, Michael Iatrou, for pointing this out.