Linux Shell script strategy (1.3), shell1.3
1.3 game variables and Environment Variables
VariableIs an essential component of any programming language, used to store various types of variables. Most scripting languagesWeak language (Dynamic Language)That is to say, when using a variable, you do not need to declare the type of the variable in advance. You only need to assign values directly.In Bash, the value of each variable is a string.No matter whether you use quotation marks when assigning values to variables, the values are stored as strings. Some special variables are reserved by the shell environment and operating system to store some special values.Environment VariableI believe that everyone is familiar with the environment variables, because even in windows, there are also environment variables.
Common variables
Common variables can be assigned and printed out in the following ways:
# Var = value # variable on the left side of the equation and var = "value" on the right side of the equation to be assigned to the variable # declare a variable var, assign a value to "value" echo $ var # use echo to output the variable value echo $ {var} # As the same line
** Note: ** var = value is different from var = value. The former is a value assignment expression, while the latter is a logical expression used to determine whether the values at both ends of the equation are the same. In the value assignment expression, if there is no blank character in the value, you do not need to use quotation marks for reference. Otherwise, you must use single or double quotation marks for variable reference. For example:
Var1 = "value" # echo $ var1 # output "value" var2 = "value 2" # blank space, use the quotation mark echo $ var2 # to output "value" var3 = value 2 # With a blank character. Do not use the quotation mark echo var3 # In ubuntu14.04, return command not found
Returns the length of a string.
The length of a string is an extremely important feature of a string. In shell, you can use the following method to obtain the length of a string:
Var = "value" length =$ {# var} echo $ length # output 7
Environment Variable
Variables are named in common naming methods. When the program starts, it accepts a family of static environment variables. You can use the env (eviroment) command to view all the terminal-related environment variables. For a process, you can use the following command to view its runtime environment variables:
Cat/proc/$ PID/environ # PID is always an integer pgrep firefox # I just returned the result 3013cat/proc/3013/environ # A Bunch Of returned results, not listed
- HTTP_PROXY environment variable
Environment variables generally do not need to be defined in the current process, but are inherited from the parent process. HTTP_PROXY environment variable, which defines which proxy server should be used by the Internet. You can set the environment variable in the following ways:
HTTP_PROXY = 192.168.1.23: 3128 export HTTP_PROXY # Use export to set Environment Variables
- PATH Environment Variable
By default, many standard environment variables are available to shell, and PATH is one of them.
Echo $ PATHecport PATH = "$ PATH;/home/user/bin" # Add a new PATH to the PATH
- SHELL Environment Variables
You can use SHELL environment variables to identify the current shell version. The method is as follows:
Echo $ SHELL # output shell version echo $0 # Same as above
- UID environment variable
UID is an important environment variable that can be used to check whether the current script is run as a Super User or as a normal user. The UID of the root user is 0.
Reference
Introduction to Linux Shell scripts