English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Linuxのdeclareコマンドはshell変数を宣言するために使用されます。
declareはshellコマンドであり、第1種文法では変数を宣言し属性を設定するために使用できます([rix]は変数の属性です)。第2種文法ではshell関数を表示するために使用されます。何も引数を指定しない場合、すべてのshell変数と関数が表示されます(setコマンドの効果と同じです)。
declare [+/-[rxi][変数名=設定値] または declare -f
パラメータの説明:
整数型変数の宣言
# declare -i ab //整数型変数の宣言 # ab=56 //変数の内容を変更 # echo $ab //変数の内容を表示 56
変数属性を変更
# declare -i ef //整数型変数の宣言 # ef=1 //変数の割り当て(整数値) # echo $ef //変数の内容を表示 1 # ef="wer" //変数の割り当て(テキスト値) # echo $ef 0 # declare +i ef //変数属性を解除 # ef="wer" # echo $ef wer
変数を只読みに設定
# declare -r ab //変数を只読みに設定 # ab=88 //変数の内容を変更 -bash: ab: 只読み変数 # echo $ab //変数の内容を表示 56
配列変数の宣言
# declare -a cd='([0]="a" [1]="b" [2]="c")' //配列変数の宣言 # echo ${cd[1]} b //変数の内容を表示 # echo ${cd[@]} //配列の全ての変数内容を表示 a b c
表示関数
# 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 }