Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Linux Shell script variables and environment variables

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve Linux Shell script variables and environment variables". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to achieve Linux Shell script variables and environment variables".

I. play change quantity and environmental variables

Variables are an essential part of any programming language and are used to store various types of variables. Scripting languages are mostly weakly typed languages (dynamic languages), that is, when using variables, you don't need to declare the type of variables in advance, you just need to assign values directly. In Bash, the value of each variable is a string. Whether or not you use quotation marks when assigning a value to a variable, the value is stored as a string. There are some special variables will be retained by the shell environment and operating system, used to store some special values, such variables are called environment variables, I believe we are no stranger to environment variables, because even in the windows operating system, there are environment variables.

Second, ordinary variables

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

The code is as follows:

# var=value # equation is the variable on the left and the value to be assigned to the variable on the right

Var= "value" # declares a variable var and assigns it to "value"

Echo $var # uses echo to output the value of a variable

Echo ${var} # functions the same as the previous line

* * Note: * * var=value is different from var=value in that the former is an 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 an assignment expression, if there are no white space characters in the value, you don't have to use quotation marks for references, otherwise you must use single or double quotes for variable references. For example:

The code is as follows:

Var1= "value" # does not contain blank characters

Echo $var1 # output "value"

Var2= "value 2" # with white space, using quotation marks

Echo $var2 # output "value"

Var3=value 2 # contains blank characters and does not use quotation marks

Echo var3 # in ubuntu14.04, return command not found

Gets the length of the string

The length of a string is a very important feature of a string. In shell, you can use the following methods to obtain the length of a string:

The code is as follows:

Var= "value"

Length=$ {# var}

Echo $length # 7 will be output here

III. Environmental variables

Variables are named in a common naming manner. When the program starts, it accepts a family of 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:

The code is as follows:

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

Pgrep firefox # my result just now returned 3013

Cat / proc/3013/environ # returned a heap without listing

HTTP_PROXY environment variable

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. This environment variable can be set in the following ways:

The code is as follows:

HTTP_PROXY=192.168.1.23:3128

Export HTTP_PROXY # uses export to set environment variables

PATH environment variable

By default, there are many standard environment variables available for 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 PATH

SHELL environment variable

Use the SHELL environment variable to identify the version of shell currently in use as follows:

The code is as follows:

Echo $SHELL # outputs the version of shell

Echo $0 # serves the same purpose 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 superuser or a normal user. The UID for the root user is 0.

Thank you for your reading, the above is the content of "how to achieve Linux Shell script variables and environment variables". After the study of this article, I believe you have a deeper understanding of how to achieve Linux Shell script variables and environment variables, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report