Shell Tutorial Variables

Source: Internet
Author: User

1.Shell variable 1.1 definition variable
Your_name= "Http://www.cnblogs.com/uniquefu"

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 name can only use English letters, numbers and underscores, the first character cannot begin with a number;
    • Cannot have spaces in the middle, you can use the underscore (_);
    • punctuation cannot be used;
    • You can't use the keywords in bash (you can see the reserved keywords using the help command).
1.2 Using variables

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

Your_name= "Http://www.cnblogs.com/uniquefu" Echo ${your_name}echo $your _name

The curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable.

It's a good programming habit to add curly braces to all the variables.

Operation Result:

[Email protected] bin]#/test.sh Http://www.cnblogs.com/uniquefuhttp://www.cnblogs.com/uniquefu

Note: To assign a value to a variable, you cannot add the dollar sign, which is $your_name= "Uniquefu"

1.3 Read-only variables

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

The following example attempts to change a read-only variable, resulting in an error:

Your_name= "Http://www.cnblogs.com/uniquefu" readonly your_nameyour_name= "https://www.baidu.com"

Operation Result:

[Email protected] bin]#/test.sh  /test.sh:line 7:your_name:readonly variable
1.4 Deleting a variable

Use the unset command to delete a variable. Grammar:

unset variable Name

The variable cannot be used again after it has been deleted. unset command cannot delete read-only variables

Instance:

Your_name= "Http://www.cnblogs.com/uniquefu" unset Your_nameecho ${your_name}

The above instance execution will not have any output, and will not error

Shell Tutorial 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.