How to Set Environment Variables in Linux

1. export PATH=$PATH:hello [only valid for current bash]
Modify the environment variable by adding a path


2. Modify the .bash_profile file in the user directory, add the path to it, and it will take effect next time you enter this user. [Valid for a single user only]
For example: define an environment variable in .bashrc, and it will take effect the next time Linux starts.
3. getenv()
gets an environment variable,

char* p=getenv("PATH");
char* new_path="PATH=aaaa";
p=new_path;

4. putenv()
changes or increases environment variables

 1 #include<stdlib.h>//putenv()需要包含的头文件
  2 int main(){
  3     char* p;
  4     if(p=getenv("PATH")){
  5     printf("PATH=%s\n",p);
  6     putenv("PATH=test");
  7     printf("PATH=%s\n",getenv("PATH"));
  8     }
  9    return 0;
 10 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728354&siteId=291194637