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

Case Analysis of Global variables, Local variables and Environmental variables in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail the "Linux global variables, local variables, environment variable instance analysis", detailed content, clear steps, details handled properly, I hope that this "Linux global variables, local variables, environment variable instance analysis" article can help you solve doubts, following the editor's ideas slowly in-depth, to learn new knowledge together.

A global variable is a global variable, which means that a variable is valid throughout the current Shell session. Each Shell session has its own scope and does not affect each other. Variables defined in Shell are global variables by default.

To actually demonstrate the irrelevance of global variables in different Shell sessions, open two Shell at the same time in a graphical interface, or use two terminals to remotely connect to the server (SSH).

First open a Shell window, define a variable an and assign it to 1, then print it, so that the value of variable a can be printed correctly in the same Shell window. Then a new Shell window opens, and the value of the variable an is also printed, but the result is empty, as shown in figure 1.

Figure 1: open two Shell sessions

This shows that the global variable an is only valid in the first Shell that defines it and has no effect on other Shell. It's easy to understand, just like Xiao Wang and Xiao Xu both have a TV set (with the same variable name), but the programs shown on Xiao Wang's and Xiao Xu's televisions can be different at the same time (variable values are different).

It is important to emphasize that the scope of global variables is the current Shell session, not the current Shell script file, and they are different concepts. Open a Shell window to create a Shell session, open multiple Shell windows to create multiple Shell sessions, each Shell session is an independent process, with a different process ID. In a Shell session, multiple Shell script files can be executed, and global variables are valid in all of these script files.

For example, there are now two Shell script files, a.sh and b.sh. The code for a.sh is as follows:

The code #! / bin/bash echo $a b=200b.sh is as follows:

#! / bin/bash echo $b opens a Shell window and enter the following command:

$axi99 $. . / a.sh 99 $. As you can see from the output, the variable a defined on the command line in the Shell session is valid in a.sh, and the variable b defined in a.sh is also valid in b.sh.

The local variable Shell also supports custom functions, but there is one difference between the Shell function and other programming language functions such as Candlestick + and Java: the variable defined in the Shell function is also a global variable by default, which has the same effect as defining variables outside the function. Take a look at the following code:

#! / bin/bash # define function function func () {aq99} # call function func # output variable echo $an inside the function output result: 99

An is defined inside the function, but its value can also be obtained outside the function, proving that its scope is global, not just inside the function.

If the scope of a variable is limited to the interior of the function, you can add the local command to the definition, and the variable becomes a local variable. Take a look at the following code:

#! / bin/bash # defines the function function func () {local aq99} # calls the function func # the output of the variable echo $an inside the output function is empty, indicating that the variable an is not valid outside the function and is a local variable.

This feature of the Shell variable is similar to the variable in JavaScript. A variable defined inside the JavaScript function is also a global variable by default, and only by adding the var keyword will it become a local variable.

The environment variable global variable is only valid in the current Shell session, and if you export it using the export command, it is also valid in all child Shell, which is called an environment variable.

The Shell in which the environment variable is created is called the parent Shell, and if another Shell is created in the parent Shell, the Shell is called the child Shell. When a child Shell is generated, it inherits the environment variables of the parent Shell for its own use, so the environment variables can be passed from the parent Shell to the child Shell. It is understandable that environment variables can also be passed to Sun Shell.

Note that environment variables can only be passed down, not up, that is, "pass on the son, not the father".

The easiest way to create a sub-Shell in a Shell is to run the bash command, as shown in figure 2.

Figure 2: enter the child Shell

You can exit Shell layer by layer through the exit command.

Here is a demonstration of the use of environment variables:

$Shell 22 # defines a global variable $echo $a # outputs an in the current Shell, successfully 22$ bash # enters sub-Shell $echo $a # outputs an in sub-Shell, fails $exit # exits sub-Shell exit $export a # exports an as an environment variable $bash # re-enters sub-Shell $echo $a # outputs an again in sub-Shell Successfully 22$ exit # exits the child Shell exit $exit # exits the parent Shell, and ends the entire Shell session. By default, an is invalid in the child Shell. After you export an as an environment variable using export, you can use it in the child Shell.

The form of export an is to export the variable an as an environment variable after defining it. If you want to export it as an environment variable at the same time, you can write export adept 22.

Note that what we have been emphasizing in this section is that the environment variable is valid in the child Shell, not that it is valid in all Shell types; if you create a new Shell through the terminal, it is not a child Shell of the current Shell, and the environment variable is not valid for this Shell.

In addition, the environment variable exported through the export command is temporary and destroyed when the Shell session is closed. Therefore, this environment variable only works locally and does not affect all Shell.

If you want the environment variable to be valid in all Shell and can be saved permanently without losing it after closing the Shell, you need to write the environment variable to the startup file.

Read here, this "Linux global variables, local variables, environment variable instance analysis" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

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