指定变量的类型: 使用declare或者typeset


指定变量的类型: 使用declare或者typeset
 
declare/typeset选项
-r 只读
declare -r var1(declare -r var1与readonly var1是完全一样的)
这和C语言中的const关键字一样, 都用来指定变量为只读. 如果你尝试修改一个只读变量的值, 那么会产生错误信息.
root@ubuntu:~/resource/shell-study/0508-2013# declare -r var1=11  
root@ubuntu:~/resource/shell-study/0508-2013# echo $var1  
11  
root@ubuntu:~/resource/shell-study/0508-2013# let "var1=12"  
bash: var1: readonly variable  
root@ubuntu:~/resource/shell-study/0508-2013# ^C  
root@ubuntu:~/resource/shell-study/0508-2013# readonly var2=12  
root@ubuntu:~/resource/shell-study/0508-2013# echo $var2  
12  
root@ubuntu:~/resource/shell-study/0508-2013# let "var2=13"  
bash: var2: readonly variable  
root@ubuntu:~/resource/shell-study/0508-2013#   
 
-i 整型
declare -i number   脚本将会把变量"number"按照整型进行处理. 
如果把一个变量指定为整型的话, 那么即使没有expr或者let命令, 也允许使用特定的算术运算.
#!/bin/bash  
  
declare -i number  
number=3  
echo "number = $number"  
  
number="123"  
echo "number = $number"   
  
number="abc"  
echo "number = $number"   
  
number=6/3  
echo "number = $number"   
  
not_int_number=6/3  
echo "not_int_number = $not_int_number"   
exit 0  
看看结果:
root@ubuntu:~/resource/shell-study/0508-2013# ./test1.sh   
number = 3  
number = 123  
number = 0  
number = 2  
not_int_number = 6/3  
root@ubuntu:~/resource/shell-study/0508-2013#  
 
-a 数组
declare -a indices  变量indices将被视为数组.
root@ubuntu:~/resource/shell-study/0508-2013# declare -a str  
root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5)  
root@ubuntu:~/resource/shell-study/0508-2013# echo $str  
1  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[0]}  
1  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[1]}  
2  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[2]}  
3  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[3]}  
4  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[4]}  
5  
root@ubuntu:~/resource/shell-study/0508-2013#   
用${数组名[下标]} 下标是从0开始 下标是:*或者@ 得到整个数组内容,直接通过 数组名[下标] 就可以对其进行引用赋值,如果下标不存在,自动添加新一个数组元素,直接通过:unset 数组[下标] 可以清除相应的元素,不带下标,清除整个数据
root@ubuntu:~/resource/shell-study/0508-2013# str=(1 2 3 4 5)  
root@ubuntu:~/resource/shell-study/0508-2013# echo $str  
1  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[*]}  
1 2 3 4 5  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}  
1 2 3 4 5  
root@ubuntu:~/resource/shell-study/0508-2013# str[1]=10  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}  
1 10 3 4 5  
root@ubuntu:~/resource/shell-study/0508-2013# unset str  
root@ubuntu:~/resource/shell-study/0508-2013# echo ${str[@]}  
  
root@ubuntu:~/resource/shell-study/0508-2013#   
 
-f 函数
declare -f
如果在脚本中使用declare -f, 而不加任何参数的话, 那么将会列出这个脚本之前定义的所有函数.
declare -f function_name
如果在脚本中使用declare -f function_name这种形式的话, 将只会列出这个函数的名字.
root@ubuntu:~/resource/shell-study/0508-2013# 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  
}  
root@ubuntu:~/resource/shell-study/0508-2013# declare -f hello  
 
-x export
declare -x var3
这句将会声明一个变量, 并作为这个脚本的环境变量被导出.
-x var=$value
declare -x var3=373
declare命令允许在声明变量类型的同时给变量赋值.

相关内容

    暂无相关文章