Linux Shell script series (3): variables and Environment Variables

Source: Internet
Author: User

Linux Shell script series (3): variables and Environment Variables

This article mainly introduces the Linux Shell script series (3): variables and environment variables. This article describes common variables, get the length of strings, environment variables, and so on, for more information, see

I. Game variables and Environment Variables

A variable is an essential component of any programming language and is used to store various types of variables. Most scripting languages are weak 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 will be retained by the shell environment and operating system to store some special values. These variables are called environment variables. I believe everyone is familiar with the environment variables, because there are environment variables even in windows.

Ii. Common variables

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

The Code is as follows:

# Var = value # the left side of the equation is the variable, and the right side is the value to be assigned to the variable.

Var = "value" # declare a variable var and assign it to "value"

Echo $ var # use echo to output the variable value

Echo $ {var} # serves the same line as above

** 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:

The Code is as follows:

Var1 = "value" # No blank characters

Echo $ var1 # output "value"

Var2 = "value 2" # contains a blank space, using quotation marks

Echo $ var2 # output "value"

Var3 = value 2 # blank characters without quotation marks

Echo var3 # In ubuntu14.04, the command not found is returned.

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:

The Code is as follows:

Var = "value"

Length =$ {# var}

Echo $ length # output 7

Iii. Environment Variables

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:

The Code is as follows:

Cat/proc/$ PID/environ # PID is always an integer

Pgrep firefox # the result I returned is 3013.

Cat/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:

The Code is as follows:

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.

The Code is as follows:

Echo $ PATH

Ecport 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:

The Code 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.

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.