Shell variable type in Linux

Source: Internet
Author: User

(Reprinted)

Shell variables include local variables, environment variables, location variables, standard variables, and special variables.

2. 1. the local variable used in the life cycle of the shell script (that is, logging on to a shell to exit, this shell is the Life Cycle) sets a local variable format: variable name = variable, display a variable: Echo $ {variable name}. You can also display all local variables: set. The local variables can also be set to read-only: readonly variable name, the variable has the read-only attribute. If you run the readonly command in shell, all the read-only variables in the current environment will be displayed.

2. 2. environment variables are used for all user processes (that is, all sub-processes under shell). This is a bit like the environment variables in Windows (system-> advanced-> environment variables ), environment variables are generally stored in $ home /. in bash_profile and/etc/profile, the former applies only to the current user, and the latter applies to all users to set an environment variable using export. Here we set an environment variable: export pagename = "ghostwwl" sets an environment variable named pagename. You can use the Env command to view the environment variable, and run the Export command to view the environment variable.

2. 3. The location variable is equivalent Program Parameters in (equivalent to argv in c). In shell, the position variable generally allows a total of 10 variables from $0 to $9. If there are more than 10 variables, you must use other methods. Obviously, $0 is the script name, and it is the subsequent parameter from $1. That's the argv.
Let's take a small example. The shell program AA. Sh is illustrated below:
#! /Bin/bash
# Named AA
Echo "program name: $0 ″
Echo "Name: $1 ″
Echo "Age: $2 ″
Echo "province: $3 ″
Echo "City: $4 ″
Echo "fifth parameter: $5 ″

Run the script./AA. Sh ghostwwl 24 Hubei Wuhan. The result is as follows:
Program name:./AA. Sh
Name: ghostwwl
Age: 24
Province: Hubei
City: Wuhan
The fifth parameter is:
It can be seen that the fifth parameter is null because it does not exist.
Of course, we can pass parameters through system commands in the script. The following is a metaphor:
Find/usr/share/realpaly-name $1-print
In this script, you can find the file whose file name is the first parameter in the/usr/share/realplay directory.
So many other parameters are used for a long time.

2. 4. standard variables, that is, some scalar values in the standard environment established in the bash environment, will be automatically parsed. They are basically defined in/etc/profile with exinit, home, ifs, LOGNAME, mail, mailcheck, etc.
Exinit is the initialization and running parameters of the VI editor.
Home is the home directory of our dummies.
Theoretically, any character can be used to separate the field fields of ifs. It is a metaphor for/usr/bin;/usr/sbin.
The IFS here is.
You can use echo $ LOGNAME to view the LOGNAME.
Mail indicates the user's email address path.
Mailcheck indicates the number of seconds at each interval to check the inbox.
There are also a bunch of standard variables, such as path, tz, PS1, ps2, PWD, etc. I will not write them all here. I will go to the materials and read them myself. I believe it should be pretty easy to find.

2. 5. Special Variables
$ # Indicates the parameter list passed to the script
$ * All parameters passed to the script are displayed as a string. Different from the location variable, there can be more than 9 parameters here.
$ Indicates the ID of the process currently running the script
$! ID of a process running in the background
$ @ Is the same as $ #, but it is enclosed by quotation marks and each parameter is returned in quotation marks.
$-Display the current options used by shell, which is the same as the SET command
$? Display the launch status of the Last Command run. 0 indicates no error, and any other value indicates an error.

3. replace variables in Shell

Here we will talk about the frequent use of variable replacement, which is actually to replace the variable value with its name. This is the case when the variable is displayed. You can use:
To test, we first define a variable Haha = "***** you"
A. $ {variable name}. (The braces here are optional, but they are usually added)
B. $ {variable name: + variable value} indicates that if the previous variable value is not empty, the variable value is displayed. Otherwise, the variable value is left empty. Echo $ {Name: + "ghostwwl"} this way, the "ghostwwl" will be displayed no matter whether you have previously assigned a value to the name"
C. $ {variable name :? Error message} This indicates that when the variable name exists, it is the value of the variable. If it does not exist, the error message following the question mark is displayed as Echo $ {age: + "no defined variable Age"} this error message is displayed because the age does not exist. Age: no defined variable Age
D. $ {variable name:-variable value} indicates that if the variable name is not set, the variable value is provided here instead. Obviously, if the variable name is set, the original value is returned: echo $ {age:-24} is replaced by 24 because the age variable is not set. If it is Echo $ {Haha: -"Hello"} the original value "*** you" is displayed here. Remember not to confuse it with the situation in B.
E. $ {variable name: = variable value} indicates that if the variable name is not set, set its value. Echo $ {age: = 24} has not set the value of age, therefore, when this sentence is run, the value is assigned and displayed. Of course, after assigning values, you can use age as a normal variable, that is, we have Haha and age variables.

These five types of data (of course, if you remove the braces in the first one, there are six types), you may not get used to it at first. If you use more, you will get used to it. Ah, haha!

4. Clear the variable (unset)
If you can define variables, you can also delete them. In this example, we want to delete the Haha, because the value is *** you, which is not very civilized. We can use: unset Haha from here we can see that the unset variable name is used to clear the variable.
Note that the read-only variables with readonly settings cannot be canceled or reset.

Let's change the previous example here:
#! /Bin/bash
# Named AA
Echo "program name: $0 ″
Echo "Name: $1 ″
Echo "Age: $2 ″
Echo "province: $3 ″
Echo "City: $4 ″
Echo "number of parameters: $ #"
Echo "script parameter: $ *"
Echo "script process ID: $"
Run the script./AA. Sh ghostwwl 24 Hubei Wuhan and the result is:
Program name:./AA. Sh
Name: ghostwwl
Age: 24
Province: Hubei
City: Wuhan
Number of Script Parameters: 4
The script parameter is: ghostwwl 24 Hubei Wuhan
Script process ID: 212

5. Some commands that affect Variables
5.1 declare create or display Variables
Parameter-F only displays the function name
Parameter-r creates a read-only variable (typeset is also supported)
Parameter-X creates a transfer variable
Parameter-I create an integer variable
If + is used in the parameter instead of-, the option has the opposite meaning.

5.2 create an environment variable for export
Parameter-indicates that the option ends, and all subsequent parameters are real parameters
The parameter-F indicates that the variable in the "variable-value" control is a function name.
The-N parameter converts a global variable to a local variable ).
Parameter-P: global variable list displayed

5.3 create or display Read-Only variables
Parameter-indicates that the option ends
Parameter-F creates a read-only variable

5.4 set or reset various shells
5.5 shift [N] is used to move or adjust the location variable so that $4 is assigned to $3 (that is, all are moved forward once). In this example, N indicates how many bits are moved, the default value is to move one digit.
5.6 typeset: This is the same as declare.
5.7 definition of unset clearing Variables
Parameter-indicates that the option ends
Parameter-F deletes Read-Only variables, but cannot specify variables and functions in the sehll environment.

6. Shell operators
6.1-bit operations ~, >>, <, &, |, ^ At first glance, we can see that they are reverse, left, right, and, Or, different or.
6.2 $ [] indicates that the value of the expression in [] is equal to $.
6.3 logical operators &, ||,>,<,= ,! = People can understand the same as C.

6.4 Value assignment operator =, + =,-=, * =,/=, % =, & =, |=, <=, >>=, ^ = similar to other languages. Of course, the expression value is let $ A = $ B + $ C.

I don't want to talk nonsense about the priority of the 6.5 operator, which is similar to that of other languages.

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.