Environment variables of linux system - find out what environment variables are

citation

In the Linux system, we use the ls command and enter ls directly on the command line. The contents of the current directory will be listed. We can find the executable file of ls in the /usr/bin/ directory. As shown below:

Insert image description here

Take a look at the following situation:
Insert image description here
Why do the above two "different" commands achieve the same function?
The reason isenvironment variablesWhat the hell! Because the ls we usually enter is actually in this directory.executable file. When we enter ls, the command line automatically finds the file for us through the "path in the environment variable"!

To verify this situation? We look at the following current environment variables:

echo $PATH

Insert image description here
You can see that the current environment variable contains the path ==/usr/bin/==. When we enter ls, we will automatically find the relevant executable file based on the path.

environment variables

Generally refers to some parameters used in the operating system to specify the operating environment of the operating system. Environment variables usually serve some special purpose and often have global properties.

Common environment variables

  • PATH: Specifies the search path for the command
  • HOME: specifies the user's home working directory
  • SHELL: Current Shell, usually /bin/bash

echo

View environment variables
echo $environment variable name

Examples below:

echo $PATH && echo $HOME
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/xty/.local/bin:/home/xty/bin
/home/xty

export

Modify environment variables
Usage: export PATH=
is equivalent to assignment operation, use: split.

Example:

export PATH=$PATH:/home/xty/cplusplus/linux/1027
解释:将PATH修改为$PATH(原来的环境变量)+自己新设置的路径。(注意路径中不能有. 否则会当成当前路径)

Insert image description here

There is an executable file under this path, which is a C language file written by ourselves.

Insert image description here
Previously we needed to execute our own files as above, but now we don't need to do this.
Witness the moment of miracle! As shown below:
Insert image description here
We can run the program just by entering the file name. This is the role of environment variables!

env

Show all environment variables

unset

Delete variable or function
unset [-option] name
f: function
v: variable a>
Insert image description here

set

Display locally defined shell variables and environment variables.

Get environment variables through code

 int main(int argv, char* argv[], char *env[])

The first parameter: represents the number of command line parameters passed in.
The second parameter: stores the pointer to the command line parameters. The size is argv
The third parameter: stores the system environment variables.

Use the third parameter to get

    1 #include<stdio.h>
    2 
    3  //通过第三个参数获取环境变量。
    4 int main(int argc, char *argv[], char *env[])                                                                                                                         
    5 {
    
    
    6   printf("begin.............\n");
    7 
    8   int i;
    9   for(i = 0; env[i]; i++)
   10   {
    
    
   11     printf("env[%d]:%s\n",i,env[i]); // 打印环境变量
   12   }
   13 
   14   printf("end.................\n");
   15 
   16 
   17 
   18   return 0;
   19 }

Use the global variable enviorn to obtain environment variables

Insert image description here
As shown in the figure above, these environment variables are stored in environ, and they store pointers to environment variables.
Example: Environment variables can also be obtained using global variables.

    1 #include<stdio.h>
    4 int main(int argc, char *argv[])                                                                                                                                      
    5 {
    
    
    6   printf("begin.............\n");
    7 
    8   extern char **environ;//声明全局变量
    9   int i = 0;
   10   for(; environ[i];i++)
   11   {
    
    
   12     printf("%s\n",environ[i]);
   13   }      

Get environment variables through system calls

System call function
getenv (environment variable name)

Run the example:

    1 #include<stdio.h>
    2 #include<stdlib.h>
    4 int main(int argc, char *argv[])
    5 {
    
    
    6 
    7   printf("%s\n",getenv("PATH"));
    8   printf("%s\n",getenv("HOME"));                                                                                                                                      
    9 	return 0;
   10 }
  
  //输出结果
  /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/xty/.local/bin:/home/xty/bin
/home/xty

Environment variables have global properties

  • The child process can inherit the environment variables of the parent process's parent process.
    Example: Why can we obtain environment variables through char *env[]?
    Because the test program we wrote was created by the parent process (bash), those environment variables belong to the parent process (bash) and are inherited by the child process. On the surface, we have nothing. Passed, in fact, the parent process automatically passed it to us.

  • Environment variables have global properties.
    The environment variables set through export have global properties.
    Take a look at the code:

    1 #include<stdio.h>
    2 #include<stdlib.h>
    4 int main(int argc, char *argv[])
    5 {
    
    
    6 
    7   printf("%s\n",getenv("XTYY")); //获取XTYY环境变量
    8 
    9   printf("\n");                                                                                                                                                       
   10 }

At first I didn't set the XTYY environment variable and we got nothing. Later, after I set it through export, I ran the program again and found that the environment variables were obtained. Therefore it can be proved that environment variables have global properties.
Insert image description here

The role of the first two parameters of the main function

 int main(int argv, char* argv[])

The first parameter: represents the number of command line parameters passed in.
The second parameter: stores the pointer to the command line parameters. size is argv

There is a sample program that can explain the function of these parameters,
Example 1:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<unistd.h>
  5 
  6 
  7 
  8 int main(int argc, char *argv[])
  9 {
    
    
 10   if(argc != 2)
 11   {
    
    
 12     printf("Usage: %s 至少要有一个选项\n", argv[0]);
 13     return 1;
 14   }
 15 
 16   if(strcmp("-a", argv[1]) == 0)
 17   {
    
    
 18     printf("这是功能一\n");
 19   }
 20   else if(strcmp("-b", argv[1]) == 0)
 21   {
    
    
 22     printf("这是功能二");
 23   }
 24 
 25   return 0;                                                                                                                                                             
 26 }
 27 

Insert image description here
From the above example, we can see that after executing the program, the command line options are stored in the argv array, thereby realizing different functions.

Example two:

  8 int main(int argc, char *argv[])
  9 {
    
    
 10   printf("argc = %d\n",argc);
 11 
 12   int i = 0;
 13   for(; i<argc; i++)
 14   {
    
    
 15     printf("%s ",argv[i]);
 16   }
 17   printf("\n");                                                                                                                                                         
 18 }

Insert image description here

It can be seen that argc represents the number of options; argv[0] represents the command; the rest represent options.

The above is the knowledge related to environment variables. We will continue to add more in the future, so please stay tuned.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/134083475