[Linux] Environment variables

Environment variable

definition

Environment variables: Variables that save operating environment parameters; make program operating environment configuration more flexible; and can realize small amount of data communication between processes (passing between parent and child processes) through environment variables

Command operation

env : view environment variables
set : view all variables (environment variables have the transitivity of parent and child processes, ordinary variables do not)
echo : view specified variables (can also be used to print strings to the terminal)

echo &MYVAL: print the value of the variable MYVAL
echo "abcdef": print the string abcdef

expor : set the environment variable
unset : delete the environment variable

Typical environment variables

PATH

The default search path for the program to run—input the command name in the command line terminal to directly execute the command program with the corresponding name. In fact, the shell captures the command name, and then goes to the path specified by the PATH environment variable to find the program. Run and report an error if it can't be found.
For example: you need to add ./ before the program name when you run the program. If you add ./ to the PATH environment variable, you can use the program name to run the program directly, which means that as long as the program is in the default path of the PATH, you can directly Run with program name

Environment variable interface

Get the value of the environment variable by the name of the environment variable :
char *getenv(const char *name);
In addition, there are two ways to achieve:
extern char**environ - Declare an existing global variable, which saves all accessible The string address of the environment variable (pointer name is immutable)
int main (int argc, char *agrv, char*env[] )-the environment variable is accessed through the third formal parameter of the main function

Set environment variables:
int setenv(const char *name, const char *value, int overwrite);
int putenv(char *string);

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/114760714