Linux Shell Script Raiders (1.3)

Source: Internet
Author: User

1.3 Playing variables and environment variables

variables are an essential part of any programming language and are used to store variables of various types. Scripting languages are mostly weak type languages (dynamic languages), meaning that when you use variables, you don't need to declare the types of variables beforehand, just assign them directly. in bash, the value of each variable is a string. regardless of whether you use quotation marks when assigning variables, the values are stored as strings. There are special variables that are reserved 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 environment variables exist even on Windows operating systems.

Common Variables

Normal variables can be assigned in the following ways and printed out:

#var=value          #等式左边是变量,右边是将要赋给变量的值var="value"         #声明一个变量var,并将其赋值为“value”echo$var           #使用echo输出变量的值echo ${var}         #作用同上一行

* * Note: **var=value is different from var = value, which is an assignment expression, while the latter is a logical expression used to determine whether the value is the same at both ends of the equation. In an assignment expression, if there is no white space character in value, you do not have to use quotation marks for reference, otherwise you must use single or double quotation marks for the variable reference. For example:

var1="value"            #不含空白符echo$var1              #输出“value”var2="value 2"          #含空白格,使用引号echo$var2              #输出“value”2            #含空白符,不使用引号echo var3               #在ubuntu14.04中,返回command not found

gets the length of the string
The length of the string is a very important feature of the string, and the length of the string can be obtained in the shell using the following methods:

var="value"length=${#var}echo$length            #此处将输出7
Environment Variables

Variables are named by common naming methods. When the program starts, he accepts a family of ring static variables and can use the env (eviroment) command to view all the terminal-related environment variables. For a process, its runtime environment variables can be viewed with the following command:

cat /proc/$PID/environ      #PID总是一个整数pgrep firefox               #我刚才的结果返回3013cat /proc/3013/environ      #返回了一堆,不列出
    • Http_proxy Environment variables
      Environment variables usually do not need to be defined in the current process, but are inherited from the parent process. The HTTP_PROXY environment variable, which defines which proxy server the Internet should use. The environment variable can be set in the following ways:
HTTP_PROXY=192.168.1.23:3128export HTTP_PROXY            #使用export来设置环境变量
    • PATH environment variable
      By default, there are many standard environment variables available to the shell, and path is one of them.
echo$PATHecport PATH="$PATH;/home/user/bin"#在PATH中添加一条新路径
    • Shell Environment variables
      Use the shell environment variable to identify the currently used shell version, as follows:
echo$SHELL          #输出shell的版本echo$0              #作用同上
    • UID environment variable
      UID is an important environment variable that can be used to check whether the current script is running as a superuser or as a normal user. The root user's UID is 0.
Reference

Linux Shell Script Raiders

Linux Shell Script Raiders (1.3)

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.