In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to set environmental variables". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to set environment variables.
Many people know about environmental variables, and many people are also using environmental variables. But when many people talk about using environment variables in Linux and macOS, the first thing that comes to mind is the form of environment variables like export XXX=yyy. But in fact, in Linux and macOS, there are three ways to define environment variables, and their effects and scope are different.
Take macOS as an example to illustrate the differences between these three environment variables. We use the following Python program to verify the setting result:
Import os name = os.getenv ('WHO_AM_I',' cannot find the environment variable') print ('the value of the environment variable WHO_AM_I is:', name)
Global environment variable
If you use ZSH, then you will know that ZSH has a configuration file ~ / .zshrc, which, when opened, contains the following contents:
Add a line of code here: export WHO_AM_I=kingname, you can set a global environment variable. This environment variable will always be in effect. Even if you turn the computer off and then open it now, or open any new terminal window, this environment variable is always valid. As shown in the following figure:
If you don't have ZSH on your computer, you can also modify the system default ~ / .bashrc file.
Environment variables valid only for the current window
In addition to modifying the configuration file, you can also execute commands directly to set environment variables, which is the most well-known way. Let's delete the environment variable in ~ / .zshrc to test it:
As you can see, when we execute export WHO_AM_I=kingname, this environment variable is only useful for the current terminal window. If you reopen a new terminal window, or close the current window and then open it, it will fail.
Environment variables that are valid only for the current command
In addition, we can also set the environment variable to be valid only for the current command, that is, to place the code that defines the environment variable directly before the executed command. And do not add export. For example, if we want to execute the command python3 test.py, then we will do this:
WHO_AM_I=kingname python3 test.py
The running effect is shown in the following figure:
As you can see, the environment variable can only be obtained correctly by the line with the definition statement in front of the command.
This approach is especially useful when using Crontab. If you use Crontab a lot, you must know that your global environment variables are not recognized in Crontab. Whether you define environment variables in .zshrc file or .bashrc, when you execute via Crontab, the program does not read environment variables. At this point, you can use this way to let the environment variables be defined on the spot while the program is running:
0 12 * cd / job/xxx & & WHO_AM_I=kingname python3. / test.py
In this way, the program can always get the environment variables correctly.
At this point, I believe you have a deeper understanding of "how to set environment variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.