First, topsy variables and environment variables
Variables are an essential part of any programming language and are used to hold variables of various types. Scripting languages are mostly weakly typed languages (dynamic languages), which means that when you use a variable, you don't have to declare the type of the variable in advance, just assign the value directly. In bash, the value of each variable is a string. The value is stored as a string, regardless of whether you use quotation marks when assigning a value to a variable. There are special variables that are retained by the shell environment and the operating system to store special values, which are called environment variables and are not unfamiliar to environment variables, because even on Windows operating systems, environment variables exist.
Second, ordinary variables
Ordinary variables can be assigned in the following ways, and print out the output:
Copy Code code as follows:
#var =value #等式左边是变量, right is the value that will be assigned to the variable
Var= "Value" #声明一个变量var and assigns it to "value"
Echo $var #使用echo输出变量的值
Echo ${var} #作用同上一行
* * Note: **var=value is different from var = value, the former is an assignment expression, and the latter is a logical expression that determines whether the values are the same at both ends of the equation. In an assignment expression, if there is no whitespace in value, you do not have to use quotation marks to refer to it, or you must use either single or double quotes for the variable reference. For example:
Copy Code code as follows:
var1= "Value" #不含空白符
echo $var 1 #输出 "value"
Var2= "Value 2" #含空白格, using quotes
echo $var 2 #输出 "value"
Var3=value 2 #含空白符, do not use quotes
Echo Var3 #在ubuntu14.04, returning command not found
Gets the length of the string
The length of the string is a very important feature of the string, and in the shell you can get the length of the string by using the following method:
Copy Code code as follows:
Var= "Value"
length=${#var}
Echo $length #此处将输出7
Third, environment variables
Variables are named using a common naming method. When the program starts, he accepts a family of ring static variables, and you can use the env (eviroment) command to view all terminal-related environment variables. For a process, its run-time environment variables can be viewed with the following command:
Copy Code code as follows:
cat/proc/$PID/environ #PID总是一个整数
Pgrep Firefox #我刚才的结果返回3013
Cat/proc/3013/environ #返回了一堆, not listed
Http_proxy Environment variables
Environment variables typically 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 the Internet should use. The environment variable can be set by the following methods:
Copy Code code as follows:
http_proxy=192.168.1.23:3128
Export Http_proxy #使用export来设置环境变量
PATH environment variable
By default, there are a number of standard environment variables available for use by the shell, and path is one of them.
Copy Code code as follows:
Echo $PATH
Ecport path= "$PATH;/home/user/bin" #在PATH中添加一条新路径
Shell Environment Variables
You can use the shell environment variable to identify the version of the shell that you are currently using, as follows:
Copy Code code as follows:
Echo $SHELL #输出shell的版本
echo $ #作用同上
UID environment variable
UID is an important environment variable that can be used to check whether the current script is running as Superuser or as a normal user. The UID of the root user is 0.