In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you what the meaning of environmental variables in linux, I hope you will learn something after reading this article, let's discuss it together!
In linux, environment variables refer to the predefined parameters of the system; they are equivalent to global variables, exist in all Shell, have inheritance, and store information about shell sessions and work environments.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
1. The concept of environmental variables.
1.1 meaning of environmental variables
Environment variables generally refer to some parameters of the specified operating environment in the operating system, that is, the predefined parameters of the system. It is equivalent to a pointer, and to see the value of a variable, you need to add "$".
Environment variables are features of bash shell that store information about shell sessions and work environments. Allows data to be stored in memory for access by programs and scripts running on shell. This data can be used to identify users, systems, characteristics of Shell, and any other data that needs to be stored.
1.2 Classification of environmental variables
1.2.1 according to the scope of action
Variables in Linux can be divided into environment variables and local variables:
1) Environment variables: equivalent to global variables, exist in all Shell and have inheritance
2) Local variables: equivalent to local variables, only exist in the current Shell. Local variables contain environment variables, while non-environment variables do not have inheritance.
1.2.2 by life cycle
1) permanent: the configuration file needs to be modified and the variable takes effect permanently.
2) temporarily: use export definition to disable Shell.
2. Organization of environmental variables.
Each program has an environment table, which is an array of character pointers, each pointing to an environment string that ends with'0'. The third argument to the Main function is the environment table address.
3. Common environmental variables
3.1 PATH
This variable specifies the search path for the command.
[example]
Explanation: you can see that in the current directory, the value of PATH. It represents the search path for each instruction executed under the current directory, with each directory separated by a colon. When an instruction is executed, the system will look for it in the system file and execute it when it is found; otherwise, it will not be executed.
3.2 HOME
This variable specifies the user's home working directory, which is the default directory when the user logs in to the Linux system.
3.2.1 main working directory under ordinary users
[example]
3.2.2 main working directory under superuser
[example]
Explanation: an environment variable is a variable whose value varies from user to user.
3.3 HISTSIZE
This variable specifies the number of history commands to save.
[example]
Explanation: a maximum of 1000 command records are kept in the Linux system.
3.4 LOGNAME
This variable specifies the login name that displays the user.
[example]
3.5 HOSTNAME
This variable specifies the hostname
[example]
3.6 SHELL
This variable specifies the parser that the user is currently using.
[example]
4. Commands to modify and display environment variables
4.1 echo
This command is used to display the value of an environment variable.
4.2 env
This command specifies that all environment variables and values are displayed.
[example]
4.3 set
This command specifies the local variable.
[example]
4.4 export
This command specifies that a new environment variable is set.
[example]
Note: environment variables are generally indicated by capitalization and underlining.
4.5 unset
This command specifies that the environment variable is cleared.
[example]
4.6 readonly
This command is used to set read-only environment variables.
[example]
Explanation: once the environment variable MY_ENV is set to read-only mode, it cannot be modified until the user logs out.
5. Files that store environment variables
5.1 / etc/profile
The purpose of this file is to get the environment variables of the system when the user logs in, only once.
5.2 / etc/bashrc
When the / etc/profile file is executed, the user will read the file if they want to open bash Shell. If you want to do something every time you open bash Shell, you can set it in this file.
5.3 ~ / .bash_profile
Each user can use this file to enter shell information dedicated to their own use. When the user logs in, the file is executed only once, and by default, it sets some environment variables to execute the user's .bashrc file.
A single user's changes to this file will only affect his subsequent login to the system. Therefore, you can set special environment variables or special operations for a single user here, and it will get these new environment variables or do some special operations every time it logs in, but only when it logs in.
5.4 ~ / .bashrc
This file contains bash information specific to an individual's bash shell, which is read when logging in and each time a new shell is opened.
The modification of this file for a single user will affect every time he logs in to the system and each time a new bash is opened. Therefore, you can set special environment variables or special operations for a single user here, so every time it logs in to the system or opens a new bash, it will get the corresponding special environment variables and special operations.
5.5 ~ / .bash_logout
The file is executed each time you exit the system (exit bash shell).
6. Get environment variables through code
[example 1]
Note: the third parameter on the command line is the environment table address, which is a character array pointer.
Running result:
[example 2] get it through the third-party variable environ
Running result:
Note: the global variable environ defined in libc points to the environment variable table, and because environ is not included in any header file, it should be declared with extern when used.
7. Get or set environment variables through system call
It is common to use getenv and putenv functions to access specific environment variables, rather than environ variables. However, if you want to view the entire environment, you must use the environ pointer.
[example 1]
Running result:
[example 2]
Running result:
Explanation: "export MY_ENV=Love" exports environment variables, which can be inherited by subroutines, while "MY_ENV=Love" exports ordinary variables and does not have inheritance (like the getenv function).
After reading this article, I believe you have a certain understanding of "what is the meaning of environmental variables in linux". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.