Linux bash shell-declare

Source: Internet
Author: User

DeclareOrTypesetBuilt-in commands (they are identical) can be used to restrict attributes of variables. This is a method that is not strictly defined in some programming languages. CommandDeclareIt is only available after bash version 2. CommandTypesetYou can also run it in the ksh script.

Declare/typeset options

-R Read-Only
   1 declare -r var1

(Declare-r var1AndReadonly var1Same role)

This is similar to C'sConstThe qualifier is the same. An operation that tries to change the value of the read-only variable will cause an error message and fail.

-I Integer
1 declare-I number 2 # the remaining part of the script treats "number" as an integer. 3 4 number = 3 5 echo "Number = $ number" # Number = 3 6 7 number = three 8 echo "Number = $ number" # Number = 0 9 # The Script tries to put the string "three" is used as an integer to evaluate the value: of course it will fail, so the value is 0 ).

Some arithmetic calculations can be completed in a variable declared as an integer without expr or let.

   1 n=6/3   2 echo "n = $n"       # n = 6/3   3    4 declare -i n   5 n=6/3   6 echo "n = $n"       # n = 2
- Array
   1 declare -a indices

VariableIndicesWill be treated as an array.

-F Function
   1 declare -f

The script does not contain any parametersDeclare-fAll functions defined before this script are listed.

   1 declare -f function_name

WhileDeclare-f function_nameOnly the specified function is listed.

-X export
   1 declare -x var3

In this way, a variable is declared as the environment variable of the script and exported.

-X var = $ value
   1 declare -x var3=373

DeclareThe command allows you to assign values to a variable while declaring the variable type.

Example 9-21. UseDeclare to declare the variable type

1 #! /Bin/bash 2 3 func1 () 4 {5 echo This is a function. 6} 7 8 declare-f # list the above functions. 9 10 echo 11 12 declare-I var1 # var1 is an integer. 13 var1 = 2367 14 echo "var1 declared as $ var1" 15 var1 = var1 + 1 # After the integer declaration, you do not need to use 'let '. 16 echo "var1 incremented by 1 is $ var1." 17 # try to change the value of a variable declared as an integer to a floating point value. 18 echo "Attempting to change var1 to floating point value, 2367.1. "19 var1 = 2367.1 # cause an error message. The value of this variable remains unchanged. 20 echo "var1 is still $ var1" 21 22 echo 23 24 declare-r var2 = 13.36 # 'desc' allows you to set attributes of a variable. 25 # + also assigns values to the variable. 26 echo "var2 declared as $ var2" # try to change the value of the read-only variable. 27 var2 = 13.37 # cause an error and exit from the script. 28 29 echo "var2 is still $ var2" # this line will not be executed. 30 31 exit 0 # The script will not exit from here.

Use built-inDeclareVariable range.

1 foo () 2 {3 FOO = "bar" 4} 5 6 bar () 7 {8 foo 9 echo $ FOO 10} 11 12 bar # print bar.

However...

1 foo () {2 declare FOO = "bar" 3} 4 5 bar () 6 {7 foo 8 echo $ FOO 9} 10 11 bar # print nothing. 12 13 14 # thanks to Michael Iatrou for pointing out this point.
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.