English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux declare command is used to declare shell variables.
declare is a shell command that can be used to declare variables and set their attributes (e.g., [rix] is an attribute of the variable) in the first syntax, and to display shell functions in the second syntax. Without any parameters, it will display all shell variables and functions (the same effect as executing the set command).
declare [+/-[rxi][variable name = set value] or declare -f
Parameter Description:
Declare Integer Variable
# declare -i ab //Declare Integer Variable # ab=56 //Change Variable Content # echo $ab //Display Variable Content 56
Change Variable Attribute
# declare -i ef //Declare Integer Variable # ef=1 //Variable Assignment (Integer Value) # echo $ef //Display Variable Content 1 # ef="wer" //Variable Assignment (Text Value) # echo $ef 0 # declare +i ef //Cancel Variable Attribute # ef="wer" # echo $ef wer
Set Variable Read-only
# declare -r ab //Set Variable as Read-only # ab=88 //Change Variable Content -bash: ab: Read-only variable # echo $ab //Display Variable Content 56
Declare Array Variable
# declare -a cd='([0]="a" [1]}="b" [2]}="c")' //Declare Array Variable # echo ${cd[1]} b //Display Variable Content # echo ${cd[@]} //Display the entire array variable content a b c
Display Function
# declare -f command_not_found_handle () { if [ -x /usr/lib/command-not-found ]; then /usr/bin/python /usr/lib/command-not-found -- $1; return $?; else if [ -x /usr/share/command-not-found ]; then /usr/bin/python /usr/share/command-not-found -- $1; return $?; else return 127; fi; fi }