Shell script Variable definition

Source: Internet
Author: User

http://blog.csdn.net/qyf_5445/article/details/8886071

Custom variables
No type distinction in bash variables
Aa=abc123 define variables and assign values abc123
Aa= defines an empty variable or empties the variable AA, but the variable still exists
Export test= "Hello World" setting environment variable test
Export or export-p Show all environment variables
Declare/typeset option Variable Name
Declare or typeset have the same function: Specify variable properties. If the use of declare is not followed by any parameters, then bash will actively call out all the variable names and contents, as if using set!
Options:
-a defines the following variables as arrays (array)
-I defines the subsequent variables as integers (integer)
-X turns the subsequent variable into an environment variable, as with export,
-R Sets the following variable to read-only, the variable cannot be changed, and it cannot be unset
-F Lists the functions in the script
ReadOnly used to set read-only variables
ReadOnly variable Name
readonly-f function name
Readonly-a Array Variables
Variable indirect reference eval var1=\$ $var 2

The shell supports custom variables.

Defining variables

When defining a variable, the variable name does not have a dollar sign ($), such as:

The code is as follows: Variablename= "value"

Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the name of the variable names must follow the following rules:

• The first character must be a letter (a-z,a-z).
• The middle cannot have spaces, you can use the underscore (_).
• Punctuation cannot be used.
• You cannot use the keywords in bash (you can view the reserved keywords using the help command).

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, such as the following:

The code is as follows:
For skill in Ada coffe Action Java
Do
echo "I am good at ${skill}script"
Done

If you do not add curly braces to the skill variable and write the echo "I am good at $skillScript", the interpreter will treat $skillscript as a variable (whose value is null) and the result of the code execution is not what we expect it to look like.

It is a good programming habit to add curly braces to all variables.

Redefining variables

A defined variable can be redefined, such as:

The code is as follows:
Your_name= "Tom"
Echo $your _name

Your_name= "Alibaba"
Echo $your _name

This is legal, but note that the second assignment can not be written $your_name= "Alibaba", the use of variables when the dollar symbol ($).

Shell script Variable definition

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.