Linux Shell Script Tutorial Series (iii): Variables and environment variables

Source: Internet
Author: User

This article mainly introduces the Linux Shell Script series (iii): Variables and environment variables, this article explained the common variable, get the length of the string, environment variables and so on, the need for friends can refer to the

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:

The code is 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:

The code is 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:

The code is 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:

The code is 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:

The code is 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.

The code is 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:

The code is 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.

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.