03. About the shell variables.

Source: Internet
Author: User

The shell supports custom variables.

Defining variables

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

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).

    • You can use an underscore (_) without spaces in the middle.

    • Punctuation cannot be used.

    • You can't use the keywords in bash (you can see the reserved keywords using the help command).

Examples of variable definitions:

hello= "World"

huawei= "Pit Daddy"

caoliu=1024

Using variables

With a defined variable, just precede the variable name with a dollar sign ($), such as:

caoliu=1024

Echo $caoliu

Echo ${caoliu}

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:

#vim for.sh

For Caoliu in Caoliu are so Niubi

Do

echo "All the world Otaku say ${caoliu}"

Done

Give executable permissions and execute

#chmod +x for.sh

#./for.sh

All the world's otaku say Caoliu.

All the world's otaku say is

All the world's otaku say so.

All the world's otaku say Niubi.

caoliu. as a variable (whose value is null), 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.






This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1793195

03. About the shell 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.