Linux: environment variables

Table of contents

1. Basic variables

2. Obtain environment variables through code

2.1 main parameter passing 

2.2 Global variable environ 

2.3 System call getenv() 

3. Add environment variables to the script file

4. Environment variables usually have global properties


1. Basic variables

Environment variablesGenerally refer to some parameters used in the operating system tospecify the operating environment of the operating system For example: When we write C/C++ code, when linking, we never know where our linked dynamic and static libraries are, but we can still link successfully and generate an executable program. The reason is that Relevant environment variables help the compiler find it. Environment variables usually have some special purposes, and they usually have global characteristics in the system.

Common environment variables

  • PATH: Specify the search path for the command
  • HOME: Specify the user's home working directory (that is, the default directory when the user logs in to the Linux system)
  • SHELL: The current Shell, its value is usually /bin/bash.
  • USER: Current account user

View environment variables method

echo $NAME //NAME: your environment variable name

env view all environment variables

Test PATH

  1. Create myproc.c file
    #include <stdio.h>
    int main()
    {
        printf("hello world!\n");
        return 0;
    }
  2. Comparison between ./myproc execution and myproc execution
  3. To execute a command, you must first find the corresponding executable program! Why can some instructions be executed directly without a path, but our binary program needs a path to execute?
  4. Add the path where our program is located to the environment variable PATH, export PATH=$PATH:myproc The path where the program is located
  5. comparison test
  6. Is there any other way to run it directly without adding a path? That is to move the executable program we wrote to the default path of PATH.​ 

By default, changing environment variables is limited to this login. If you log in again, the environment variables will be automatically restored. So why is this?

Because the values ​​of these environment variables are stored in the configuration file, the configuration file will be re-read every time you log in.

Test HOME

  • Use root and ordinary users to execute echo $HOME respectively to compare the differences. Execute cd ~; pwd to see the relationship between ~ and HOME

 It can be found that different users have different home directories, namely HOME.

Why is the default directory for ordinary users /home/xxxx, while root is /root???

When logging in

  1. Enter username&&password
  2. Certification
  3. Form environment variables (certainly more than one, PATH, PWD, HOME)
  4. According to the user name, initialize HOME. root: HOME=/root, ordinary user: HOME=/home/xxxx
  5. cd $HOME

There will be a large number of environment variables in the system. Each environment variable has its own special purpose and is used to complete specific system functions!

2. Obtain environment variables through code

There are three ways to obtain environment variables:

2.1 main parameter passing 

When the system starts our program, we can choose to provide two tables to our process (main)

  1. Command line parameter list
  2. Environment variable table

The command line parameter list has been discussed in previous articles. Let’s talk about the environment variable table here. We execute the following code and use the third parameter of the main function

#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;
}

operation result:

2.2 Global variable environ 

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. It’s okay not to declare it. Use the third-party variable environ to obtain environment variables:

#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;
}

2.3 System call getenv() 

Get or set environment variables through system calls: getenv() function to access specific environment variables

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

3. Add environment variables to the script file

Principle of environment variable generation:

The processes started by the command line are all child processes of shell/bash, The command line parameters and environment variables of the child process are the parent process bash Pass it on to us! The environment variable information of the parent process exists in the form of a script configuration file!

What we change directly on the command line 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 and form its own environment variable table information!

Every time you log in, your bash process will read .bash_profile. The content in the configuration file forms an environment variable table information for our bash process!

If you want an environment variable you want to create to exist every time you log in, you can use ll -a in the home directory to view the hidden .bash_profile and modify it.

 Add the environment variables you created:

Check whether it is successful: 

4. Environment variables usually have global properties

  • Environment variables usually have global properties and can be inherited by child processes.

We create a locally defined variable OURENV in the command line and run the following code

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

operation result: 

 It can be found that only by adding local variables to the environment variables through export can they be obtained by the sub-process myproc of the command line bash.

Local variables vs environment variables

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

If we set PATH to an empty string, we can find that some instructions can be run, but some instructions cannot be run.

Linux command categories:

  • Regular commands, the shell allows the child process to be executed through fork, such as ls touch, so it cannot be executed after the PATH is empty.
  • Built-in command, a function of the shell command line, can directly read local variables defined inside the shell to pwd and cd.

Commands related to environment variables

  1. echo: display the value of an environment variable
  2. export: Set a new environment variable
  3. env: displays all environment variables
  4. unset: clear environment variables
  5. set: Display locally defined shell variables and environment variables

Variables without export are local variables. It can be displayed by set and cleared by unset.

This article is over!

Guess you like

Origin blog.csdn.net/qq_72916130/article/details/134896232