[Linux Systematic Learning] Command line parameters | Understanding environment variables again

=========================================================================

Click to go directly to personal homepage:Xiaobai is not a program Yuan

Linux 专栏:Linux system chemistry 习

Gitee:Gitee

=========================================================================

Table of contents

main function passes parameters to obtain environment variables

Manually add environment variables

Export environment variables

environ gets environment variables

The difference between local variables and environment variables

Linux command classification

standing orders

built-in commands

Environment variable related instructions


main function passes parameters to obtain environment variables

In the last article, we introduced that the main function of C/C++ has two parameters. In fact, the main function also has a third parameter that is used to obtain our environment variables.

   1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 #include<sys/types.h>
    5 #include<unistd.h>
    6 int main(int argc ,char * argv[],char * env[])
    7 {
    8     int i=0;
    9     for(;env[i];i++)
   10     {
   11         printf("pid: %d , env[%d]:%s\n",getpid(),i,env[i]);                                
   12     }
   13 }

 

The left side is the result we get using the command env, and the right side is the result we get by running the above; comparing the content obtained by the two, we will find that they are the same. In fact, when we use commands to start a process, it is a child process of shell/bash, so the command line parameters and environment variables of the child process are passed to us by the parent process.

The parent process can indeed pass environment variables to the child process, which means that the parent process has environment variables. But where do the environment variables of the parent process shell/bash come from?

To solve this problem, we need to review the content of the previous article. In the previous article, we mentioned that environment variables can be modified. What will happen if we accidentally modify it wrongly? You might as well give it a try.

After simulating misoperation, we will find that some basic instructions cannot be used. Don't worry, we just need torestart our xshell.

Through this small experiment we can prove:

  • Environment variable information exists in the form of script configuration files.
  • What we change is the environment variable information inside the bash process. Every time we log in again, a new bash interpreter will be formed for us, and the new bash interpreter will automatically read the environment variable configuration file to form its own environment variable information.

This configuration file is ahidden file that exists in our home directory.

So the above problems are all explained:Every time you authenticate and log in, bash will read the contents of the .bash_profile configuration file and generate an environment variable information table for bash. .


Manually add environment variables

There is no instruction to add environment variables under Linux. You can directly enter them on the command line according to the environment variable format customized in the system.

The environment variables we set manually are actually local variables and are not in the system's environment variable table.

Export environment variables

export+environment variable name+environment variable content

However, the environment variables we exported only exist in the environment variable table in the bash process, and they still do not exist after logging in again after logging out.​ 

Therefore we want to add our own environment variables to the configuration file mentioned above.

 

The above also shows you a kind oferror:Do not include spaces.

System environment variables have global properties and will be inherited by all processes.


environ gets environment variables

There are two global variables in C language, one iserror (error code) and the other isenviron , where environ is a secondary pointer pointing to the address of the first string in the environment variable table.

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<sys/types.h>
  5 #include<unistd.h>
  6 int main()
  7 {
  8     extern char**environ;
  9     int i=0;
 10     for(;environ[i];i++)
 11     {
 12         printf("%s\n",environ[i]);                                                           
 13 
 14     }
 15 }


Based on the previous article, we introducedthree ways to obtain environment variables

  • gettenv
  • main function passing parameters
  • approximately

The difference between local variables and environment variables

  • Local variables are only valid within bash and will not be inherited by child processes.
  • Environment variables are global by allowing all child processes to inherit them!

Linux command classification

Most of the commands cannot be executed after we misoperate the environment variables. These commands are regular commands; but the echo command can still be executed, and these commands are built-in commands.

standing orders

The shell creates a child process and lets the child process execute.

built-in commands

A function on the shell command line can of course directly read local variables defined within the shell.


Environment variable related instructions

  • echo: display the value of an environment variable
  • export: Set a new environment variable
  • env: displays all environment variables
  • set: Display locally defined shell variables and environment variables
  • unset cancels environment variables

This article combines the previous article to introduce the command line parameters and environment variables in Linux; we will cover them again in subsequent articles. I hope you will gain a lot after reading it. You can also comment on the content of the article and share your own opinions in the comment area. Your support is the driving force for me to move forward. Thank you all for your support! ! !

Guess you like

Origin blog.csdn.net/qq_55119554/article/details/134864458