"Go" shell tutorial--06 Shell variable: Shell variable definition, delete variable, read-only variable, variable type

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:

    1. 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:

    1. Myurl="http://see.xidian.edu.cn/cpp/linux/"
    2. MyNum=
Using variables

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

    1. Your_Name="Mozhiyan"
    2. echo $your _name
    3. echo ${your_name}

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:

    1. For skill in Ada coffe Action Java
    2. Do
    3. echo "I am Good at ${skill}script"
    4. 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:

    1. Myurl="http://see.xidian.edu.cn/cpp/linux/"
    2. echo ${myurl}
    3. Myurl="http://see.xidian.edu.cn/cpp/shell/"
    4. echo ${myurl}

This is legal, but note that the second assignment can not be written $myUrl = "http://see.xidian.edu.cn/cpp/shell/", the use of variables when the dollar symbol ($).

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:

    1. #!/bin/bash
    2. Myurl="http://see.xidian.edu.cn/cpp/shell/"
    3. ReadOnly Myurl
    4. Myurl="http://see.xidian.edu.cn/cpp/danpianji/"

Run the script with the following results:

/bin/sh:name:this variable is read only.
Delete a variable

Use the unset command to delete a variable . Grammar:

    1. Unset variable_name

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

As an example:

    1. #!/bin/sh
    2. Myurl="http://see.xidian.edu.cn/cpp/u/xitong/"
    3. Unset Myurl
    4. echo $MYURL

The above script does not have any output.

Variable type

When you run the shell, there are three different variables:

1) Local Variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

2) Environment variables

All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

3) Shell variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

"Go" shell tutorial--06 Shell variable: Shell variable definition, delete variable, read-only variable, variable type

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.