linux declare命令參數及用法詳解

功能說明:聲明 shell 變量。
   語 法:declare [+/-][afrix]   補充說明:declare爲shell指令,在第一種語法中可用來聲明變量並設置變量的屬性([rix]即爲變量的屬性),在第二種語法中可用來顯示shell函數。若不加上任何參數,則會顯示全部的shell變量與函數(與執行set指令的效果相同)。
  參 數:
  +/- "-"可用來指定變量的屬性,"+"則是取消變量所設的屬性。
   -a 定義爲數組array
   -f 定義爲函數function
   -i 定義爲整數integer
   -r 定義爲只讀
   -x 定義爲通過環境輸出變量
示例1: 聲明整數型變量
[email protected]:~# declare -i ab //聲明整數型變量
[email protected]:~# ab=56 //改變變量內容
[email protected]:~# echo $ab //顯示變量內容
56

示例2:改變變量屬性
[email protected]:# declare -i ef //聲明整數型變量
[email protected]:# ef=1  //變量賦值(整數值)
[email protected]:m# echo $ef //顯示變量內容
1
[email protected]:~# ef="wer" //變量賦值(文本值)
[email protected]:~# echo $ef
0
[email protected]:~# declare +i ef //取消變量屬性
[email protected]:~# ef="wer"
[email protected]:~# echo $ef
wer
[email protected]:~#


示例3:設置變量只讀

[email protected]:~# declare -r ab //設置變量爲只讀
[email protected]:~# ab=88 //改變變量內容
-bash: ab: 只讀變量
[email protected]:~# echo $ab //顯示變量內容
56
[email protected]:~#


示例4: 聲明 數組變量

[email protected]:~# declare -a cd='([0]="a" [1]="b" [2]="c")' //聲明數組變量
[email protected]:~m# echo ${cd[1]}
b //顯示變量內容

[email protected]:~# echo ${cd[@]} //顯示整個數組變量內容
a b c

示例5: 顯示函數

[email protected]:~# 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
}
[email protected]:~/linuxso.com#

擴展閱讀:更詳細的使用示例 linuxso.com整理

 
示例一 declare是內建命令

[[email protected]]# type -a declare
declare is a shell builtin
[[email protected]]#

[[email protected]]# type -a typeset
typeset is a shell builtin
[[email protected]]#

資料整理:www.linuxso.com
示例二 declare -i之後可以直接對表達式求值

[[email protected]]# x=6/3
[[email protected]]# echo $x
6/3
[[email protected]]# declare -i x
[[email protected]]# echo $x   
6/3
[[email protected]]# x=6/3
[[email protected]]# echo $x
2

如果變量被聲明成整數,可以把表達式直接賦值給它,bash會對它求值。

[[email protected]]# x=error
[[email protected]]# echo $x
0

如果變量被聲明成整數,把一個結果不是整數的表達式賦值給它時,就會變成0.

[[email protected]]# x=3.14
-bash: 3.14: syntax error: invalid arithmetic operator (error token is ".14")
如果變量被聲明成整數,把一個小數(浮點數)賦值給它時,也是不行的。因爲bash並不內置對浮點數的支持。
[[email protected]]#

[[email protected]]# declare +i x

此命令的結果是取消變量x的整數類型屬性。
[[email protected]]# x=6/3
[[email protected]]# echo $x
6/3

因爲變量x不是整型變量,所以不會自動對表達式求值。可以採用下面兩個方式。

[[email protected]]# x=$[6/3]
[[email protected]]# echo $x
2
[[email protected]]# x=$((6/3))
[[email protected]]# echo $x 
2
[[email protected]]#

 
示例三 聲明只讀變量

[[email protected]]# declare -r r
[[email protected]]# echo $r

[[email protected]]# r=xxx
-bash: r: readonly variable
[[email protected]]# declare -r r=xxx
-bash: declare: r: readonly variable
[[email protected]]# declare +r r  
-bash: declare: r: readonly variable
[[email protected]]#
[[email protected]]# declare +r r
-bash: declare: r: readonly variable
[[email protected]]#
[[email protected]]# unset r
-bash: unset: r: cannot unset: readonly variable
[[email protected]]#

 
示例四 聲明數組變量(實際上,任何變量都可以當做數組來操作)

[[email protected]]# declare -a names
[[email protected]]# names=Jack
[[email protected]]# echo ${names[0]}
Jack
[[email protected]]# names[1]=Bone
[[email protected]]# echo ${names[1]}
Bone
[[email protected]]# echo ${names}
Jack
[[email protected]]# echo ${names[*]}
Jack Bone
[[email protected]]# echo ${#names}
4

直接引用names,相當於引用names[0]
[[email protected]]# echo ${#names[*]}
2

[[email protected]]# echo ${names[@]}
Jack Bone
[[email protected]]# echo ${#names[@]}
2

[[email protected]]# declare -p names  
declare -a names='([0]="Jack" [1]="Bone")'
[[email protected]]#

 
示例五 顯示函數定義

[[email protected]]# declare -F
declare -f add_jar
declare -f add_jar2
declare -f add_jar3
[[email protected]]# declare -f
add_jar ()
{
    [ -e $1 ] && CLASSPATH=$CLASSPATH:$1
}
add_jar2 ()
{
    if [ -e $1 ]; then
        CLASSPATH=$CLASSPATH:$1;
    else
        if [ -e $2 ]; then
            CLASSPATH=$CLASSPATH:$2;
        fi;
    fi
}
add_jar3 ()
{
    if [ -e $1 ]; then
        CLASSPATH=$CLASSPATH:$1;
    else
        if [ -e $2 ]; then
            CLASSPATH=$CLASSPATH:$2;
        else
            if [ -e $3 ]; then
                CLASSPATH=$CLASSPATH:$3;
            fi;
        fi;
    fi
}
[[email protected]]# declare -f add_jar
add_jar ()
{
    [ -e $1 ] && CLASSPATH=$CLASSPATH:$1
}
[[email protected]]# declare -F add_jar
add_jar
[[email protected]]# declare -F add_http://www.linuxso.com/command/declare.html
[[email protected]]# declare -F add_*
[[email protected]]# declare -F 'add_*'
[[email protected]]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章