How does a LInux application process environment variables in the current running environment?

Source: Internet
Author: User
Article Title: How does a LInux application process environment variables in the current running environment. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
How does a Linux application process environment variables in the current running environment?
  
Every program running in GNU/Linux has a so-called runtime environment. The environment here is actually a set of variables. All variables and their values are expressed in string form. By conversion, variable names are usually expressed in uppercase.
  
Everyone is familiar with environment variables. for example, we are familiar with the HOME environment variable, which indicates the path of the current user's personal user directory. For example, the PATH environment variable indicates the PATH list of the execution files searched by the current Linux system.
  
For the BASH shell program commonly used in Linux, it has its own method for managing environment variables. For example, you can use $ NAME to access the corresponding environment variables and use export to set the environment variables. The following is an example:
  
[Hwang @ langchao linuxprogram] $ echo $ HOME
/Home/hwang
[Hwang @ langchao linuxprogram] $ export MYNAME = "Luster"
[Hwang @ langchao linuxprogram] $ echo $ MYNAME
Luster
[Hwang @ langchao linuxprogram] $
  
In our application, if you need to access an environment variable, we can use the getenv () function. the getenv function is declared in this header file. The input parameter of this function is the name of the variable to be accessed. the returned value is a string. if the Accessed environment variable is not set, NULL is returned. If you need to set an environment variable in the program, you can use the setenv function. to clear a specific environment variable, use the unsetenv function. Their function prototypes are as follows:
  
# Include
Char * getenv (const char * name );
Int setenv (const char * name, const char * value, int overwrite );
Void unsetenv (const char * name );
  
It is worth mentioning that the gnu c function library contains a special global variable named environ, which is of the char type **, it points to a list of all environment variables and ends with NULL. Each string is in the form of "VALUE = value. For example, the following program can print all the environment variables in the current running environment:
  
# Include
Extern char ** environ;
Int main ()
{
Char ** var;
For (var = environ; * var! = NULL; ++ var)
Printf ("% s
", * Var );
Return 0;
}
  
Note: If you need to modify an environment variable, do not directly modify the environ variable. Instead, use a management function such as setenv or unsetenv to do this.
  
Generally, when a program starts, it copies the environment variables owned by the "parent" program that starts it. Environment variables are commonly used by GNU programs to configure the behavior of an application. for example, environment variables are used to set the HTTP proxy server address and port number. The following is a demo of code:
  
# Include
# Include
Int main ()
{
Char * server_name = getenv ("SERVER_NAME ");
If (server_name = NULL)
Server_name = server.my-company.com ";
Printf ("accessing server % s
", Server_name );
  
Return 0;
}
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.