Shell variables are used to store the specific parameters (values) that the system and the user need to use, and these parameters can vary according to the user's settings or changes in the system environment.
"The role of variables"
Provides specific parameters for flexible management of Linux systems, with two layers of meaning:
1. Variable name: Using a fixed name, by the system preset or user-defined
2. Variable value: Can change according to user's settings and system environment
"Type of variable"
1. Custom variables: defined, modified and used by the user
2. Environment variables: maintained by the system for setting the working environment
3. Positional variables: Passing parameters to the script through the command line
4. Predefined variables: A class of variables built into bash that cannot be modified directly
"Custom Variables"
Format: variable name = variable Value
Variable names start with a letter or underscore, are case-sensitive, and are recommended for all caps
(i) Define new variables
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/89/63/wKiom1gRojWA8WNRAAAoSXlz1_8235.png "title=" 1.png " alt= "Wkiom1grojwa8wnraaaosxlz1_8235.png"/>
(ii) Viewing and referencing the value of a variable
Format:echo $ Variable Value
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/89/63/wKiom1gRo9HRjIqCAAAzZyI80YQ772.png "title=" 2.png " alt= "Wkiom1gro9hrjiqcaaazzyi80yq772.png"/>
When the variable name is easily confused with the other word typeface, you need to add braces {} to enclose it, otherwise you will not be able to determine the correct variable name.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/64/wKiom1gRpbbTYkAvAAAw121eQ0c477.png "title=" 3.png " alt= "Wkiom1grpbbtykavaaaw121eq0c477.png"/>
(c) Special operations for assigning variables
1. Double quotation marks ("): Allows you to refer to other variable values through the $ symbol, for example:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/89/61/wKioL1gRp7ejtduDAAA7ShSs3Ik223.png "title=" 4.png " alt= "Wkiol1grp7ejtdudaaa7shss3ik223.png"/>
2. Single quotation mark ('): Prohibit reference to other variable values, $ as ordinary characters, for example:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/89/64/wKiom1gRqF3zd06MAAAzWjHx9y8884.png "title=" 5.png " alt= "Wkiom1grqf3zd06maaazwjhx9y8884.png"/>
3. Reverse apostrophe (') command substitution, extract output after command execution, for example:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/62/wKioL1gRrFCTP8yMAAAp1lt4bmE099.png "title=" 6.png " alt= "Wkiol1grrfctp8ymaaap1lt4bme099.png"/>
$ () function: Can replace the anti-apostrophe, solve the nesting problem, because the anti-apostrophe can not be nested, for example: Query the package that provides the USERADD command program installed configuration file location
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/89/65/wKiom1gRrQ-R1GcZAAArhI4p06Q253.png "title=" 7.png " alt= "Wkiom1grrq-r1gczaaarhi4p06q253.png"/>
4. Read command: used to prompt the user to enter information
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/89/62/wKioL1gRrcKw9y9kAAAxUy-7-0g460.png "title=" 8.png " alt= "Wkiol1grrckw9y9kaaaxuy-7-0g460.png"/>
(iv) Setting the scope of the variable
The newly defined variables are valid only in the current shell, and if you want to enter a new shell environment, you will need to export the variables as "global variables" using the Export command.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/89/62/wKioL1gRr1OhM1jrAABP8atE2eg446.png "title=" 9.png " alt= "Wkiol1grr1ohm1jraabp8ate2eg446.png"/>
You can specify more than one variable name as a parameter (you do not need to use the $ symbol), and the variable name is separated by a space
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/89/62/wKioL1gRsK-B2Q6KAABDYRhSdSo943.png "title=" 10.png "alt=" Wkiol1grsk-b2q6kaabdyrhsdso943.png "/>
When exporting global variables using export, you can also assign values to variables, such as:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/62/wKioL1gRsZaSSQTHAAAuij1CdxU023.png "title=" 11.png "alt=" Wkiol1grszassqthaaauij1cdxu023.png "/>
(v) operation of numerical variables
Format:expr variable 1 operator variable 2 [operator variable 3] ...
+: addition operation
-: Subtraction operation
\*: Multiplication, note that you cannot use only the "*" symbol, or you will be treated as a file wildcard
/: Division operation
%: Modulo operation, also known as take-rest operation, is used to calculate the remainder after dividing the value
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/89/65/wKiom1gRs_uT6K0xAABT2i2YOA4229.png "title=" 12.png "alt=" Wkiom1grs_ut6k0xaabt2i2yoa4229.png "/>
To assign the result of an operation to another variable, you can combine the command substitution operation (with the inverse apostrophe)
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/65/wKiom1gRtMzDg2-aAAArtQKDvm0323.png "title=" 13.png "alt=" Wkiom1grtmzdg2-aaaartqkdvm0323.png "/>
"Special shell variables"
1. Environment variables
Environment variables refers to a class of variables that are created in advance by the Linux system for operational needs, primarily for setting up the user's working environment, including the user's host directory, the command lookup path, the user's current directory, the login terminal, and so on.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/89/63/wKioL1gRthfxQBPoAABjzxkOUMU589.png "title=" 14.png "alt=" Wkiol1grthfxqbpoaabjzxkoumu589.png "/>
The phth variable is used to set the default search path for executable programs, for example:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/89/64/wKioL1gRyHqyyM5zAABdukDmL6Q820.png "title=" 15.png "alt=" Wkiol1gryhqyym5zaabdukdml6q820.png "/>
2. Position variables
Positional variables also become positional parameters, expressed using $1,$2,$3,..., $9. For example:
Write a adder2num.sh small script that is used to operate two integers and
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/89/67/wKiom1gRyefRCFB1AAAs5iRw5V8660.png "title=" 16.png "alt=" Wkiom1gryefrcfb1aaas5irw5v8660.png "/>
The two integers that need to be evaluated are provided in the form of positional variables when executing a script
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/67/wKiom1gRy7WgSYy7AABGs6E55T8891.png "title=" 17.png "alt=" Wkiom1gry7wgsyy7aabgs6e55t8891.png "/>
3. Pre-defined variables
Predefined variables, after the system has been installed, can not be created, can only be used, for example: $ #表示命令行中位置参数的个数, $* represents the contents of all positional parameters, $? Indicates the return state after the previous command was executed, returning 0 is correct. $ A represents the name of the currently executing script or program.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/89/67/wKiom1gRz1eSCEydAACmbGb3ZWk067.png "title=" 18.png "alt=" Wkiom1grz1esceydaacmbgb3zwk067.png "/>
This article is from the "Dark Cloud Technology" blog, please be sure to keep this source http://wuyunkeji.blog.51cto.com/12028005/1866423
Linux Learning Diary-using Shell variables