宣告弱型別變數

declare 是 bash 的內部命令。 (內部命令使用幫助顯示聯機幫助頁)。它用於顯示和定義變數或顯示函式體。

語法: 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