Linux environment variable operation function

Source: Internet
Author: User

1. extern char **environ; global variables

All the values of the Linux environment variables are stored in this global variable.

Example: Print out all the environment variables

#include <stdio.h>#include <stdlib.h>extern char **environ;Import this global variableint main (int argc, char* agrv[]) {int i;for (i=0;environ[i];i++) {printf ("%s\n", Environ[i]);}return 0;}

2. getenv get the value of an environment variable

Char *getenv (const char *name); //name Name, environment variable is the form of a key-value pair, and its value is obtained by name

Return value: Returns the corresponding value successfully, failing to return null.

3. setenv setting the value of the environment variable

int setenv (const char *name, const char *value, int overwrite); //Set, name key, value, overwrite if existing overwrite

Return value: 0 successfully returned, failure returned-1.

int unsetenv (const char *name);Clears the value of the environment variable for the specified name

Return value: 0 successfully returned, failure returned-1.

int clearenv (void); //Clear All environment variables

The return value returned successfully to 0, and the failure returned non 0.

int putenv (char *string); //change or add an environment variable, in the form of a string parameter Name=value

The return value returned successfully to 0, and the failure returned non 0.

Linux environment variable operation function

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.