De help -m declare :
NAME
declare- Set variable values and attributes.SYNOPSIS
declare[-aAfFgilnrtux] [-p] [name[=value] ...]DESCRIPTION
Set variable values and attributes.
Declare variables and give them attributes. If no NAMEs are given, display the attributes and values of all variables.
Options:
-frestrict action or display to function names and definitions
-Frestrict display to function names only (plus line number and source file when debugging)
-gcreate global variables when used in a shell function; otherwise ignored
-pdisplay the attributes and value of each NAME
Options which set attributes:
-ato make NAMEs indexed arrays (if supported)
-Ato make NAMEs associative arrays (if supported)
-ito make NAMEs have the ‘integer’ attribute
-lto convert NAMEs to lower case on assignment
-nmake NAME a reference to the variable named by its value
-rto make NAMEs readonly
-tto make NAMEs have the ‘trace’ attribute
-uto convert NAMEs to upper case on assignment
-xto make NAMEs export
Using ‘
+’ instead of ‘-’ turns off the given attribute.Variables with the integer attribute have arithmetic evaluation (see the
letcommand) performed when the variable is assigned a value.When used in a function,
declaremakes NAMEs local, as with thelocalcommand. The ‘-g’ option suppresses this behavior.Exit Status:
Returns success unless an invalid option is supplied or a variable assignment error occurs.SEE ALSO
bash(1)
IMPLEMENTATION
GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
Portanto, declare é usado para definir valores de variáveis e atributos .
Deixe-me mostrar o uso de dois atributos com um exemplo muito simples:
$ # First Example:
$ declare -r abc=ok
$ echo $abc
ok
$ abc=not-ok
bash: abc: readonly variable
$ # Second Example:
$ declare -i x=10
$ echo $x
10
$ x=ok
$ echo $x
0
$ x=15
$ echo $x
15
$ x=15+5
$ echo $x
20
No exemplo acima, acho que você deve entender o uso da variável declare sobre a variável normal! Esse tipo de declare ção é útil em funções, loops com scripts.
Visite também as Variáveis de digitação: declarar ou compor