Linux environment variable settings and access

  
 

Environment variables are closely related to the shell, and a shell is launched when the user logs in to the system. For Linux it is generally bash, but you can also reset or switch to another shell. Depending on the release version, bash has two basic system-level configuration files: /etc/bashrc and /etc/profile. These configuration files contain two different sets of variables: shell variables and environment variables. The former is only fixed in a specific shell (such as bash), which is fixed in different shells. This article is based on the article at the end of the article to streamline, organize, non-Tianyuan original, hereby declare. First, the environment variable settings 1, display environment variables $ echo $HOME example uses echo to display the variable HOME. Other variables are similarly used. 2, set a new environment variable $ export MYDIR = & rdquo; /usr /local /include & rdquo; set MYDIR to /usr /local /include3, display all environment variables $ env4, use the set command to display all locally defined shell variables $ set5 Use the unset command to clear the environment variable $ unset $MYDIR to delete the environment variable MYDIR. 6. Add to the system PATH example to modify the .bash_profile file: $ vi .bash_profile #Modify the environment variable definition file and edit your PATH statement in the format: PATH=$PATH:<PATH 1>:<PATH 2>: ---:<PATH N> Just separate the newly added path from the other path by a colon. 7. Immediately take effect. $source.bash_profile 2. Environment Variable Access The following example uses a C program to access and set environment variables. We have three functions to set or access an environment variable. 1.getenv() accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string. If the accessed environment variable does not exist, it will return NULL. 2.setenv () in the program to set a function of a certain environment variable. 3.unsetenv() clears the function of a particular environment variable. In addition, there is a pointer variable environ that points to a list of all environment variables. The following program can print out all the environment variables in the current running environment: #include <stdio.h>extern char**environ;int main (){char**var;for (var =environ;*var !=NULL ;++var)printf (”%s /n “,*var);return 0;

}

Copyright © Windows knowledge All Rights Reserved