Linux-environment variable setting, viewing and testing


1. Basic concepts

Environment variables: generally refer to some parameters used in the operating system to specify the operating environment of the operating system

For example: when we are writing C/C++ code, we never know where our linked dynamic and static libraries are when we link, but we can still link successfully and generate executable programs because there are related environment variables to help compile Finder to find.

Common environment variables

  • PATH: Save the search path of the executable program (each path is :separated by symbols)
  • SHELL: Save the name of the command line interpreter (BASH in Linux, generally in the /bin/bashdirectory)
  • HOME: Specify the home directory where the current directory is saved

Fragment knowledge: use which + [命令]to view the path of the command

2. View environment variables

  • echo $[环境变量]: You can view the path where the environment variable is located

For example: view the path of the environment variable BASH

Insert picture description here

  • env: View all environment variables in the operating system (existing in the form of key-value pairs)

Insert picture description here

3. Set environment variables

First of all, there is a fixed paradigm every time an environment variable is set :
export [环境变量的名称] = $[环境变量名称] : [新增路径]

For example, you want to add the path of PATH to the current path.
Insert picture description here
However, the environment variables set here are only temporarily effective, that is to say, there are two types of environment variable settings: temporary effective and permanent effective.

1. Provisional entry into force

The export command is executed in the current command line and will only take effect in the current terminal.

2. Permanently effective

  • Write the export command to the ~/.bashrcor ~/.bash_profilefile, these two files are environment variable files
  • After writing to the above file, you can use sourc ~/.bashrcor sourc ~/.bash_profilereload the environment variable file


When a new terminal is opened, the contents of the environment variable file will be read, and only written to the two files mentioned above, each time the terminal is started, it will be loaded into it.

4. Code verification of environment variables

First of all, we all know that in C language programs, the main function always has corresponding parameters, such as:

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

  • int argc: holds the number of command line parameters
  • char* argv[ ]: is an array of pointers, each element in the array is char*, which holds the content of specific command line parameters
  • char* env[ ]: save the specific content of the environment variable, what you need to know is that its organization format is that the last position of the array always saves NULL

Insert picture description here
Each program will receive an environment table, the environment table is an array of character pointers, each pointer points to an environment string ending with'\0'

Code test:

The code is as follows: the
Insert picture description here
results of the operation are as follows: it
Insert picture description here
can be seen that argc will also calculate itself when calculating the command line parameters

5. Obtain environment variables through code

  • The char* env[]method just mentioned above
  • getenvGet the value of a specific environment variable through a function
  • environGet the value of environment variables through parameters

among them:

char * getenv(const char * name);

  • name: the name of the environment variable
  • Returns the value of the environment variable
  • The header file is: <stdlib.h>

extern char ** approximately;

  • The environ parameter is defined in the C library
  • 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.
  • The header file of extern is: <unistd.h>

Guess you like

Origin blog.csdn.net/weixin_43937101/article/details/115285610