Detailed explanation of environment variables in Linux operating system

Table of contents

1. Basic concepts of environment variables

2. View environment variables

3.PATH

4. HOME for root and ordinary users 

5. Commands related to environment variables 

6. How environment variables are organized

6.1 Obtain environment variables through code

6.2 Obtained through the third-party variable environ

6.3 Obtain environment variables through system calls

6.4 Setting environment variables through system calls


1. Basic concepts of environment variables

Environment variables generally refer to some parameters used in the operating system to specify the operating environment of the operating system. For example: when we write C/C++ code and link, we never know the dynamic and static variables we are linking to. No matter where the library is, you can still link successfully and generate an executable program. The reason is that there are relevant environment variables to help the compiler find it. Environment variables usually have some special purpose, and they usually have global properties in the system.

2. View environment variables

echo $NAME //NAME:你的环境变量名称

3.PATH

Why can some instructions be executed directly without a path, but our binary program needs a path to execute?

We can add the path of our program to the environment variable PATH, export PATH=$PATH:the path of the hello program

 The specific operations are as follows

Configure environment variables first

The source command is usually used to re-execute the newly modified initialization file so that it takes effect immediately without waiting to log out and log in again. Its function is the same as the dot symbol "."

Then we can execute the program in any path

4. HOME for root and ordinary users 

5. Commands related to environment variables 

The startup process of environment variables in Linux

1. echo: 显示某个环境变量值
2. export: 设置一个新的环境变量
3. env: 显示所有环境变量
4. unset: 清除环境变量
5. set: 显示本地定义的shell变量和环境变量
export [-fnp] [变量名称key] = [变量值value]
#参数说明
-f:代表[变量名称]中为函数名称
-n:删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中
-p:列出所有的shell赋予程序的环境变量

There are three types of variable environments in the shell: internal variables, environment variables, and user variables.

  • Internal variables: provided by the system, do not need to be defined, and cannot be modified;
  • Environment variables: provided by the system, no need to define, can be modified, use export to convert user variables into environment variables;
  • User variables: user-defined and can be modified;

When you execute a program in a shell, the shell will provide a set of environment variables. When a shell command window is opened, it means that a shell environment is loaded.

  • export is used to set or display environment variables;
  • export can add, modify or delete environment variables;
  • The scope of export is limited to the connection login operation established this time;
    export syntax:

set and env can also display environment variables.

  • set is used to display local variables and environment variables
  • env is used to display environment variables
  • export is used to display and set environment variables

  • set displays the variables of the current shell (local variables), including the variables of the current user (environment variables)
  • env displays the variables of the current user (environment variables)
  • export displays shell variables (environment variables) currently exported as user variables.

6. How environment variables are organized

Each program will receive an environment table. The environment table is an array of character pointers. Each pointer points to an environment string terminated by '\0'.

6.1 Obtain environment variables through code

#include <stdio.h>
int main(int argc, char *argv[], char *env[])
{
 int i = 0;
 for(; env[i]; i++){
 printf("%s\n", env[i]);
 }
 return 0;
}

6.2 Obtained through the third-party variable environ

#include <stdio.h>
int main(int argc, char *argv[])
{
 extern char **environ;
 int i = 0;
 for(; environ[i]; i++){
 printf("%s\n", environ[i]);
 }
 return 0;
}

The global variable environ defined in libc points to the environment variable table. environ is not included in any header file, so it must be declared with extern when using it.

6.3 Obtain environment variables through system calls

#include <stdio.h>
#include <stdlib.h>
int main()
{
 printf("%s\n", getenv("PATH"));
 return 0;
}

6.4 Setting environment variables through system calls

//

Guess you like

Origin blog.csdn.net/m0_74234485/article/details/132567912