Specify the variable type: declare or typeset

Source: Internet
Author: User

Specify the type of the variable: Use the declare or typeset declare/typeset option-r read-only declare-r var1 (declare-r var1 and readonly var1 are exactly the same) this is the same as the const keyword in C language. It is used to specify the variable as read-only. if you try to modify the value of a read-only variable, an error message is generated. 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 integer declare-I number script will process the variable "number" according to the integer type. if you specify a variable as an integer, specific arithmetic operations are allowed even if there is no expr or let Command. #! /Bin/bash declare-I 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 check the result: 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 array declare-a indices variable indices will be treated as an array. 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 # use $ {array name [subscript]} To subscript from 0: * or @ to get the entire array content, the array name [subscript] can be used to reference and assign values to it. If the subscript does not exist, a new array element is automatically added. The corresponding elements can be cleared directly through the unset array [subscript, clear the entire data root @ ubuntu: ~ Without subscript :~ /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 function declare-f if declare-f is used in the script without adding any parameters, all functions previously defined in this script will be listed. declare-f function_name if declare-f function_name is used in the script, only the name of this function will be listed. 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} root @ ubuntu :~ /Resource/shell-study/0508-2013 # declare-f hello-x exportdeclare-x var3 will declare a variable and be exported as the environment variable of the script. -x var = $ valuedeclare-x var3 = 373declare command allows you to assign values to a variable while declaring the variable type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.