Shell programming (1) --- Variables

Source: Internet
Author: User

A variable is a memory space. When a variable is referenced by a variable name, it is called a named memory space.

To define a variable, assign a fixed memory space to the variable. assign a value to the variable, that is, fill the data in the memory space.


Variable type: defines the storage format and length of data in advance. The storage space and format used by different data types are different.

Variable types include:

Character type. Value Type. It can be classified into integer, floating point date, and time type. Boolean.


Logical operation: with (and), or (OR), non

And: & as long as one is false, the result is false. The result is true only when both are true. Or: | if one of them is true, the result is true. The result is false only when both are false. Non :!


Variable Assignment Method: var_name = vaule


Bash variable type

Note: 1. When the script is executed, a sub-shell process is started under the current shell. 2. scripts started in the command line inherit the environment variables of the current shell. 3. The script automatically started by the system (not started by the command line) needs to customize various required environment variables.

Environment variable: declared environment variable format export varname = value scope: Current Shell Process and its sub-process.

Local variable: declare the format of the local variable varname = value. Scope: valid for the entire script process or the entire bash process.

Local variable: declare the local link format local varname = value. Scope: valid only for the current code segment.

Location variable: $ N, the nth location variable, which references the location of parameters in the script. If n is greater than 10, it must be written as $ {n}

Special variables: Bash built-in variables. For example, $ ?, The returned value of the execution status of the previous command is saved (range: 0-255). If it is 0, the command is executed correctly. If it is not 0, the execution fails. 127 and are reserved for the system. Other values can be customized.


Reference variable: $ {var_name}. If the variable name is not confused, {} can be omitted.

Example:

[[email protected] ~]# ANIMAL=tiger[[email protected] ~]# echo "There is a $ANIMAL"There is a tiger[[email protected] ~]# echo "There is a ${ANIMAL}"There is a tiger[[email protected] ~]# echo "There are some $ANIMALs"There are some [[email protected] ~]# echo "There are some ${ANIMAL}s"There are some tigers[[email protected] ~]#


Environment variable:

Example

[[email protected] ~]# NAME=frame[[email protected] ~]# export NAME[[email protected] ~]# echo $NAMEframe[[email protected] ~]# bash[[email protected] ~]# echo $NAMEframe[[email protected] ~]#
Cancel variable: unset variable name


Example:

[[email protected] ~]# echo $NAMEframe[[email protected] ~]# unset NAME[[email protected] ~]# echo $NAME[[email protected] ~]#
View the variables in the shell: (including environment variables and local variables) set to view the environment variables in the current shell: printenvenvexport


Add variable content:

Example 1:

[[email protected] ~]# ANIMALS=pig[[email protected] ~]# echo $ANIMALSpig[[email protected] ~]# ANIMALS=$ANIMALS:tiger[[email protected] ~]# echo $ANIMALSpig:tiger[[email protected] ~]# ANIMALS=$ANIMALS:sheep[[email protected] ~]# echo $ANIMALSpig:tiger:sheep[[email protected] ~]#


Example 2:

[[Email protected] ~] # Echo $ path/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin:/root/bin [[email protected] ~] # Path = $ path:/tmp [[email protected] ~] # Echo $ path/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bin:/root/bin:/tmp [[email protected] ~] # Path =/Home: $ path [[email protected] ~] # Echo $ path/Home:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin: /usr/sbin:/usr/bin:/root/bin:/tmp [[email protected] ~] # Note: path is the variable name, and $ path is the content of the referenced PATH variable.


Variable definition rules:

1. It cannot be the same as the environment variable in the system.

2. The variable name can only contain letters, numbers, and underscores, and cannot start with a number.

3. It is best to be knowledgeable.


This article is from the "Hezhang" blog, please be sure to keep this source http://hezhang.blog.51cto.com/1347601/1435278

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.