Understanding environment variables

Posted by kwong on Wed, 05 Jan 2022 08:41:04 +0100

catalogue

I Basic concepts

1. Common environment variables

II command

1. How to view environment variables

2. Other relevant directives

3. Test PATH

III Organization of environmental variables

IV Get environment variables through code

V Get environment variables through system call

Vi Environment variables usually have global attributes

VII Environment variables in windows system

I Basic concepts

When we use the Linux system, the commands we use are essentially executable programs. But you didn't find a problem. We do not know where the executable program is when executing this command, and no path is allowed when executing this command. However, when the executable generated by our own code runs, we need to bring the path. This is the environment variable at work.

  • Environment variables generally refer to some parameters used in the operating system to specify the operating environment of the operating system.
  • Environment variables usually have some special purposes and usually have global characteristics in the system.

To put it bluntly, environment variables are variables with certain global properties in the system, usually to meet the needs of some systems. Indicates where some important directories of the operating system are.

Let's take a look at my environment variables under linux:

We find that there are some variables in the environment variables, and the following contents are some paths and values.

1. Common environment variables

  • PATH: Specifies the search PATH for the command
  • HOME: Specifies the user's HOME working directory (that is, the default working directory when logging in to the Linux system)
  • Sheet: current sheet. Its value is usually / bin/bash in centos

II command

1. How to view environment variables

  • Command "echo $NAME", NAME environment variable NAME.

2. Other relevant directives

  • export: set a new environment variable
  • env: displays all environment variables

  • unset: clear environment variable

  • set: displays locally defined shell variables and environment variables

3. Test PATH

  • Create a source file test c
  1 #include<stdio.h>
  2 
  3 int main(){
  4   printf("hello test1\n");                                                                                                                               
  5 
  6   return 0;
  7 }
  • How to call system commands without adding paths when calling environment variables, but the executable program we write needs adding paths?

The generated program we write is generally related to the PATH environment variable. The paths provided by the system are saved under a PATH of the PATH environment variable. When a command is executed, the system will find the executable program under the PATH of the PATH environment variable. If the executable program is found, the command will be executed. If it is not found, an error will be reported.

The environment variable is not only PATH, but also has many other functions.

  • How to realize that some of our programs can be executed without paths like commands?

1. Add the absolute PATH of the executable program to the PATH path. (unique to linux, the PATH to restart and add is gone)

2. Add the executable program to the directory of a PATH in PATH. (not recommended, command under pollution system)

  • How are these system commands to environment variables?

When installing the software, the executable program of the corresponding software will be copied to a PATH. PATH is only one of the environment variables, and the system has many environment variables to solve different scenarios.

III Organization of environmental variables

  • The environment variable is actually an array of character pointers, and each character pointer points to an environment string. This array of pointers ends with NULL.

IV Get environment variables through code

 

Here's an explanation. In fact, our main function has three parameters, and the third parameter is our environment variable.

ps supplement: command line parameters are added after the executable program

    //Display command line parameters
    1 #include<stdio.h>
    2 
    3 int main(int argc,char *argv[],char *env[]){
    4   for(int i=0;i<argc;i++){
    5     printf("argv[%d]: %s\n",i,argv[i]);                                                                                                                
    6   }
    7   return 0;
    8 }

The command line parameter is actually an array of character pointers, which do not end in null. A string that points to one command line argument. As above:

Therefore, those ls -a instructions can realize different operations through judgment:

    1 #include<stdio.h>
    2 #include<string.h>                                                                                                                                     
    3 
    4 int main(int argc,char *argv[],char *env[]){
    5   char *s=argv[1];//A command line argument must be passed
    6   if(strcmp(s,"-a")==0){
    7     printf("hello -a\n");
    8   }
    9   else{
   10     printf("hello other\n");
   11   }
   12   return 0;
   13 }

Note: even if no parameters are passed in the main function, these three parameters still exist, and the system gets the value of the call.

  • Obtained by the third parameter of the main function
    1 #include<stdio.h>
    2 #include<string.h>
    3 
    4 int main(int argc,char *argv[],char *env[]){
    5   for(int i=0;env[i];i++){
    6     printf("%s\n",env[i]);                                                                                                                             
    7   }
    8   return 0;
    9 }

As shown in the command env

  • Obtained through third-party variables

The global variable envron defined in libc(c library function) points to the environ ment variable table. Envron is not included in any header file, so it needs to be proved by extern.

    1 #include<stdio.h>
    2 #include<string.h>
    3 
    4 int main(int argc,char *argv[],char *env[]){
    5   extern char **environ;
    6   for(int i=0;environ[i];i++){
    7     printf("%s\n",environ[i]);                                                                                                                         
    8   }
    9   return 0;
   10 }

The same results can still be obtained.

V Get environment variables through system call

  • Getenv (stored in the header file stdlib.h), accesses specific environment variables, and returns 0 if the search fails
  1 #include<stdio.h>
  2 #include<stdlib.h>                                                                                                                                       
  3 
  4 int main(){
  5   printf("%s\n",getenv("PATH"));
  6   return 0;
  7 }

Vi Environment variables usually have global attributes

  • Environment variables usually have global properties and can be inherited by child processes
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 int main(){
  5   char *env=getenv("MYVAL");
  6   if(env){
  7     printf("%s\n",env);
  8   }
  9   return 0;                                                                                                                                              
 10 }

Direct operation:

        

When I add MYVAL environment variable in bash process

        

Reason: the environment variable added in bash can also be accessed by its child process test1, which is global.  

VII Environment variables in windows system

 

 

 

Topics: Linux