Section 52.1: 声明弱类型变量

declare 是 bash 的内部命令。(内部命令使用 help 来显示 “manpage”)。它用于显示和定义变量或显示函数体。

格式: declare [options] [name[=value]]...

# options are used to define
# an integer
declare -i myInteger
declare -i anotherInt=10
# an array with values
declare -a anArray=( one two three)
# an assoc Array
declare -A assocArray=( [element1]="something" [second]=anotherthing )
# note that bash recognizes the string context within []

# some modifiers exist
# uppercase content
declare -u big='this will be uppercase'
# same for lower case
declare -l small='THIS WILL BE LOWERCASE'

# readonly array
declare -ra constarray=( eternal true and unchangeable )

# export integer to environment
declare -xi importantInt=42

您还可以使用 “+”来删除给定的属性。这些选项大多无用,只是为了完整。

要显示变量和/或函数,也有一些选项

# printing definded vars and functions
declare -f

# restrict output to functions only
declare -F # if debugging prints line number and filename defined in too