Shell's declare defining variables

Source: Internet
Author: User

Experimental environment
Command description

The declare and typeset commands are bash's built-in commands (BUILTIN commands), both of which function exactly the same way to set variable values and properties.
Typeset is now deprecated and replaced by declare to see the Help manual:

~]# help typesettypeset: typeset [-aAfFgilrtux] [-p] name[=value] ...    Set variable values and attributes.        Obsolete.  See `help declare‘.
~]# help declaredeclare: declare [-aAfFgilrtux] [-p] [name[=value] ...]    Set variable values and attributes.

Command options

The option parameters for typeset and declare are generic and are described below with declare:

Declare variables and give them attributes. If no NAMEs is given, display the attributes and values of all variables.
declare [-aaffgilrtux] [-P] [Name[=value] ...]

Options:
-f [name]:列出之前由用户在脚本中定义的函数名称和函数体;-F [name]:仅列出自定义函数名称;-g name:在shell函数中可创建全局变量;-p [name]:显示指定变量的属性和值;-a name:声明变量为普通数组;-A name:声明变量为关联数组(支持索引下标为字符串);-i name :将变量定义为整数型(求值结果仅为整数,否则显示为0);-r [name[=value]] 或 readonly name:将变量定义为只读(不可修改和删除);-x name[=value] 或 export name[=value]:将变量设置为环境变量;

PS: Use + to cancel the definition of a variable type, such as canceling an integer variable definition declare +i name.

Unset Name: Cancels the property and value of the variable, except for the read-only variable.
Unset values and attributes of shell variables and functions.

Using the example
#!/bin/bashecho "Set a custom Function-func1" echofunc1 () {echo this is a function.} echo "Lists the function body." echo "=============================" Declare-f echoecho "Lists the function name." echo "=============================" Declare-f echodeclare-i var1 # var1 are an Integer.var1=2367echo "var1 declar Ed as $var 1 "var1=var1+1 # Integer declaration eliminates the need for ' let '. echo" Var1 incremented by 1 is $var 1. " # Attempt to change variable declared as Integer.echo "attempting to change var1 to floating point value, 2367.1." var1=2367.1 # Results in error message, with the change to Variable.echo "Var1 is still $var 1" echodeclare-r var2=13.  # ' Declare ' permits setting a variable property #+ and simultaneously assigning it A Value.echo "Var2 declared as $var 2" # Attempt to change readonly Variable.echoecho "Change the var2 's values to 13.37" V AR2=13.37 # generates error message, and exit from SCript.echo "Var2 is still $var 2" # This line would not Execute.exit 0 # Script won't exit here .
Reference links

Declare Advanced bash-scripting Guide

Shell's declare defining variables

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.