Shell scripts define variables and reassign _linux shell

Source: Internet
Author: User

The shell supports custom variables.

Defining variables

When you define a variable, the variable name is not a dollar sign ($), such as:

Copy Code code as follows:

Variablename= "Value"

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

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

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

Copy Code code as follows:

For skill in Ada coffe Action Java
Todo
echo "I am good at ${skill}script"
Done

If the skill variable is not curly braces, written as echo "I am good at $skillScript", the interpreter takes $skillscript as a variable (its value is empty), and the code execution results are not what we expect.

It is a good programming practice to recommend all variables plus curly braces.

Redefining variables

Defined variables that can be redefined, such as:

Copy Code code 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 write $your_name= "Alibaba", the use of variables to add the dollar ($).

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.