Set the environment variable putenv () setenv () getenv () in Linux ()

Source: Internet
Author: User
Set the environment variable putenv () setenv () getenv () (zz) in Linux)

Getenv (get environment variable content)
Putenv, setenv, unsetenv
Header file # include <stdlib. h>
Define the function char * getenv (const char * Name );
Function Description getenv () is used to obtain the content of the parameter name environment variable. The parameter name is an environment variable.
If the variable exists, a pointer to the content is returned. Environment Variable
The format is name = value.
If the returned value is successfully executed, a pointer to the content is returned. if the name of the conforming environment variable is not found
Returns null.
Example # include <stdlib. h>
Mian ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
}
Run user = root
Putenv (change or add environment variables)
Related functions: getenv, setenv, and unsetenv
Header file # include4 <stdlib. h>
Defines the int putenv (const char * string) function );
Function Description putenv () is used to change or add environment variables. The format of the string parameter is
Name = value. If the environment variable originally exists, the variable content will depend on the parameter
String is changed. Otherwise, the parameter content will become a new environment variable.
If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.
The error code enomem memory is insufficient and a new environment variable space cannot be configured.
Example # include <stdlib. h>
Main ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
Putenv ("user = test ");
Printf ("User + 5s/N", getenv ("user "));
}

Run user = root
User = root
Setenv (change or add environment variables)
Related functions: getenv, putenv, and unsetenv
Header file # include <stdlib. h>
Defines the int setenv (const char * Name, const char * value, int
Overwrite );
Function Description: setenv () is used to change or add environment variables. The parameter name is the environment variable name.
It is called a string.
The value parameter is the variable content, and overwrite is used to determine whether to change the existing
Environment variable. If the value of overwrite is not 0 and the environment variable already exists
The content is changed to the variable content specified by the parameter value. If overwrite is 0
If the environment variable already exists, the parameter value is ignored.
If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.
Error code: insufficient enomem memory, unable to configure new environment variable space
Example # include <stdlib. h>
Main ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
Setenv ("user", "test", 1 );
Printf ("user = % s/n", getenv ("user "));
Unsetenv ("user ");
Printf ("user = % s/n", getenv ("user "));
}
Run user = root
User = test
User = (null)

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.