Shell BASICS (II)-shell Variables

Source: Internet
Author: User

1. Define Variables

1) when defining a variable, the variable name does not contain the dollar sign ($), for example:
Var = "Hello World"
2) Note that there is no space between the variable name and equal sign, which may be different from all programming languages you are familiar. Variable names must follow the following rules:
The first character must be a letter (a-Z, A-Z ).
There cannot be spaces in the middle, and you can use underscores (_).
Punctuation cannot be used.
Bash keywords cannot be used.
Var01= 100
Var02 = "100"
Note that both variables store strings. Our shell is an interpreted language. Unlike C, Java needs to be compiled.

 

2. Use Variables

To use a defined variable, you only need to add the dollar sign ($) before the variable name, such:

Echo $ varecho $ {var} # This is recommended.

The curly braces outside the variable name are optional and can be added without adding them. The curly braces are used to help the interpreter identify the boundary of the variable, for example:

Filename = "lottu" Echo "this is $ {filename} 01" If brackets are not added to the filename variable, Echo "this is $ filename01 ",

The interpreter regards filename01 as a variable (its value is null), and this filename01 is not defined.
The code execution result is not what we expect.

 

3. Redefinition of Variables

 

The Defined variables can be redefined, for example:

abc="lottu"echo ${abc}abc="hello world"echo ${abc}

4. Read-Only variables

The readonly command can be used to define a variable as a read-only variable. The value of a read-only variable cannot be changed. It is a constant in other languages. For example, lottu03.sh

#!/bin/bashvar01="20"echo ${var01}readonly var01var01="10"

The running result is as follows:

$ ./lottu03.sh 20./lottu03.sh: line 5: var01: readonly variable

5. delete variables

You can use the unset command to delete variables. Syntax:
Unset varname
After a variable is deleted, it cannot be used again. The unset command cannot delete Read-Only variables.

6. Show all variables

Let's take a look at the variables in this user, including the environment variables. Local variables: Set # the previously defined var, ABC is here.
Show only environment variables: ENV
Convert local variables to environment variables and use the command Export
Delete environment variables. It is no different from local variables. Use the unset command.

7. indirect reference of Variables

Variable: + word: If the variable is defined, word is returned without changing the value of variable.
Variable: = value: Assign the value to the unassigned variable.
Variable:-value: Assign the value to the unassigned variable without changing the value of variable.

: = And:-are two common symbols. See the following example.

Example 1: First assign a color value to black and then output $ {color: = blue} and $ {color:-blue}. The output results are the same as those of black, this is because the color variable has been assigned ": =" and ":-" without resetting its value. Note: When the above two symbols are used, you must enclose them in curly brackets. Otherwise, shell will process the entire string of color: = blue as a variable name.

$colour=black$echo "The background is ${colour:=blue}" The background is black$echo "The background is ${colour:-blue}" The background is black

Example 2: Clear the color variable values, and then output the values of $ {color: = blue}, $ color, $ {color:-blue}, and $ color respectively. The result is displayed: the $ {color: = blue} and $ {color:-blue} values are both blue, because no two symbols are assigned to the color. However, after $ {color:-blue} is called, the color is still blank, that is, the ":-" symbol is not actually stored in the color, and ": = "symbol stores blue.

$ Unset color $ echo "the background is $ {color:-blue}" the background is blue $ echo $ color $ echo "the background is $ {color: = blue} "the background is blue $ echo $ color # Pay Attention to the comparison with blue

 

8. Special variables: Shell $0, $ #, $ *, [email protected], $ ?, $ And command line parameters

First declare: Here, $0, $1-$9 should not be confused with awk; their meaning is completely different.

Variable meaning
$0 file name of the current script
$ N refers to the parameter passed to the script or function. N is a number that represents the number of parameters. For example, the first parameter is $1, and the second parameter is $2.
$ # Number of parameters passed to the script or function.
$ * All parameters passed to the script or function.
[Email protected] All parameters passed to the script or function. When enclosed by double quotes (""), it is slightly different from $.
$? The exit status of the previous command or the return value of the function.
$ ID of the Current Shell Process. Shell scripts are the process IDs of these scripts.
[Note] For $ N; if n is greater than 9, add clothes {}. Otherwise it will be frozen, and no one will know. For example, ${10 }. I knew it was the old ten. On the contrary, you are old 1 + 0 or old 10. I will think about this.

For $? : Obtains the exit status of the previous command. The exit status is the result returned after the previous command is executed. Remember the execution result of the previous command or the condition judgment result. It means that your condition judgment has a bunch of commands, and only the last one is used; for example, ll; CD Md. Is the result returned by the CD Md.
The exit status is a number. Generally, if most commands are successfully executed, 0 is returned. If the command fails, 1 is returned.
Boolean indicates that 0 indicates the state of success; non-0 indicates the state of failure. This is the opposite of awk.

 

 

Shell BASICS (II)-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.