one ,Linux Classification of system environment variables

Divided by the life cycle of the variable ,Linux Variables can be divided into two categories :

** environment variable **: Also known as global variables , Exist in all Shell in ; It is permanent , And has inheritance , Namely son Shell Will inherit the father Shell Environment variables for ;

** Local variable **: current Shell Variables in , Obviously, local variables must contain environment variables ;Linux Non environment variables of local variables are not inherited .

two ,Linux Files related to system environment variables

When you enter the system ,Linux Will read in the environment variables of the system for you , These environment variables are stored in different environment variable files ,Linux The system reads the environment variable file in the following order to enter the environment variable .

2.1 System environment variable :/etc/profile

This file is the environment variable of the system , It sets the environment information for each user , When the user logs in for the first time , The file is executed , And from /etc/profile.d Search profile in directory shell Settings for . This file is a file that any user will read after logging in to the operating system ( If the user's shell yes csh,tcsh,zsh, This file will not be read ), Environment variables used to get the system , Read only once when logging in .

2.2 /etc/bashrc

After execution /etc/profile After content , If the user's Shell Running is bash, Then read /etc/bashrc file . in addition , Every time a new one bash
shell When opened , The file is read once . therefore , If you want everyone to use bash Every new user opens one bash And perform certain actions every time you log in , Or define some new environment variables for them , You can set it in this .

2.3 User environment variable ~/.bash_profile

Each user can use this file to pass in a file dedicated to their own use shell information . When a user logs in , This file is executed only once , By default , It sets some environment variables , Execute user's .bashrc file . Modification of this file in a single user directory , It will only affect every login of the user to the system in the future . therefore , You can set special environment variables or special operations for a single user here .

2.4 ~/.bashrc

This file contains information specific to a single person bash
shell of bash information , When logging in and opening a new one at a time shell Time , The file was read . A single user's modification of this file will affect every time he logs in to the system and every time he opens a new one bash. therefore , You can set special environment variables or special operations for a single user here .

2.5 ~/bash_logout

When the user exits the system every time ( sign out bash shell) Time , Execute the file .

three ,Linux Common environment variables of the system

stay Linux In the system , Common environment variables and their meanings are shown below :

PS1 : Basic prompt , about root yes #, For ordinary users $.

MAIL : Mail storage directory of the current user ;

PATH : decided Shell In which directories will I look for commands or programs ;

HOME : Current user home directory ;

SHELL : Current user Shell type ;

HISTSIZE : Number of history records ;

LOGNAME : Login name of the current user ;

LANGUGE : Language related environment variables , Multiple languages can modify this environment variable ;

HOSTNAME : Refers to the name of the host ;

four ,Linux Environment variable modification method

4.1 modify /etc/profile file

use VI In file ==/etc/profile== Add in , Delete or modify variables , This variable will Linux Valid for all users under , And it's permanent .

be careful : After modifying the file , Must run “# source /etc/profile” Before it takes effect , Otherwise, it will only take effect when you log in next time .

4.2 modify ~ Directory .bash_profile file

use VI In the user directory ==.bash_profile== Add to file , Delete or modify variables , This variable will only be valid for the current user , And it's permanent ;

be careful : After modifying the file , Must run “# source /etc/profile” Before it takes effect , Otherwise, it will only take effect when you log in next time .

4.3 use export Command to set environment variables

stay Shell Directly use the following method to define variables under the command line of :

==$ export Variable name = Variable value ==

This variable is only available in the current Shell(BASH) Or its son Shell(BASH) This is valid ,Shell Closed , Variables fail , Open new Shell There is no such variable when , If it needs to be used, it needs to be redefined .

$ export HELLO=”hello!”

$ echo $HELLO

hello!

five , Modify environment variables by command

5.1 use echo Command to view individual environment variables

View a specified environment variable .

$ echo $PATH

/usr/lib/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/study/bin:/bin/3.4.5/bin

5.2 use env/printenv Command to view all environment variables

Used to print all environment variables .

$ env

HOSTNAME=localhost.localdomain

TERM=xterm

SHELL=/bin/bash

HISTSIZE=1000

KDE_NO_IPV6=1

SSH_CLIENT=192.168.6.9 58178 5200

QTDIR=/usr/lib/qt-3.3

QTINC=/usr/lib/qt-3.3/include

SSH_TTY=/dev/pts/1

USER=study

……

5.3 use set View all locally defined environment variables

Used to display and set the current local variables . Use alone set, All variables of the current environment are displayed , It must include environmental variables and some non environmental variables .

$ set

BASH=/bin/bash

BASH_ARGC=()

BASH_ARGV=()

BASH_LINENO=()

BASH_SOURCE=()

BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release"
[5]="i686-redhat-linux-gnu")

BASH_VERSION='3.2.25(1)-release'

COLORS=/etc/DIR_COLORS.xterm

COLUMNS=131

……

5.4 use unset Deletes the specified environment variable

Used to clear variables , Whether this variable is an environment variable or a local variable , It can be cleared . If no value is specified , Then the value of the variable is set to NULL.

$export HELLO=”hello”

$ unset HELLO

#echo $HELLO

5.5 use export Command to change environment variables

Used to make the variable current shell And its son shell Environment variables for , Its current deposit period is current shell And its son shell, So after logging in again , The environment variable it set disappears . To make environment variables permanent , Then modify several environment variable configuration files introduced in Section 2 .

5.6 use readonly Command to set read-only environment variables

If used readonly Command words , Variables cannot be modified or cleared .

$ export TEST=”Test…”

$ readonly TEST

$ unset TEST

-bash: unset: Test: cannot unset: readonly variable

$ TEST=”New”

-bash: TEST: readonly variable

5.7 use source Command to make the script in the current shell Medium execution

When executing a script directly , In fact, it's in a child shell Environmental operation , That is, a sub shell To execute this script , After the script is executed , This son shell Auto exit . of course , We can also use source Command to make the script in the current shell Medium execution . as :

$ cat test.sh # View script content

echo $myname

$ echo $myname # View variables

ilonng

$ set | grep myname # Variables are local variables

myname=ilonng

$ env | grep myname # Variable is not an environment variable

$ sh test.sh # Xinkaizi shell implement , Local variables that are not environment variables are not inherited , Zai Zi shell Invisible in

# Nothing output

six , Modify environment variables through system call

In writing Linux In the process of application , Applications often use Linux Some environment variables of the system , Therefore, it is necessary to obtain system environment variables from the system ; But sometimes an application uses its own configured environment variables ( For example, the environment of the child process is different from that of the parent process ), You need to modify the environment variables of this program . This article mainly introduces how to obtain the system environment variables from the system and how to set the environment variables of this program (
Environment variables that do not affect the system ) Use of functions :getenv(),putenv(),setenv(),unsetenv().

Using the above functions requires the following header files :

#include

6.1 Get system environment variables :getenv()

【 Function definition 】:char *getenv(const char *name);

【 Function description 】:getenv() Used to get parameters name Contents of environment variables ;

【 Parameter description 】:name Is the name of the environment variable , The format of the environment variable is name=value; If the variable exists, a pointer to the value of the environment variable is returned , That is, point value Pointer to ; If the variable does not exist , Then return NULL Pointer ;

【 Return value 】: Execution success returns a pointer to the value of the environment variable , If no matching environment variable name is found, the NULL;

【 Model example 】:

#include

#include

int main(void)

{

char *p;

p = getenv("PATH");

if(p == NULL)

{

printf("Get env failed.\n");

return -1;

}

printf("PATH=%s\n",p);

return 0;

}

6.2 change / Add environment variable :putenv()

【 Function definition 】:char *putenv(const char *string);

【 Function description 】:putenv() Used to change or increase the content of environment variables ;

【 Parameter description 】:string The format is “name=value”, If the environment variable previously existed , Then the variable content will be changed according to the parameters string change , Otherwise, the content of this parameter will become a new environment variable ;

【 error code 】:ENOMEM, insufficient memory , Unable to configure new environment variable space ;

【 Return value 】: Successful execution return 0, Return when an error occurs -1;

【 Model example 】:

#include

#include

int main(void)

{

int ret;

char *p;

p = getenv("DB_HOME");

if(p == NULL)

{

printf("Getenv failed.\n");

return -1;

}

printf("DB_HOME=%s\n",p);

ret = putenv("DB_HOME=/opt/www/cgi-bin/");

if(ret != 0)

{

printf("Putenv failed.\n");

return -1;

}

p = getenv("DB_HOME");

if(p == NULL)

{

printf("Getenv failed.\n");

return -1;

}

printf("After putenv:DB_HOME=%s\n",p);

return 0;

}

6.3 change / Add environment variable :setenv()

【 Function definition 】:char *setenv(const char *name,const char *value,int overwrite);

【 Function description 】:setenv() Used to change or increase the content of environment variables ;

【 Parameter description 】:name: String for the name of the environment variable ;value: Is the content of the environment variable ;overwrite: Used to decide whether to change existing environment variables : If overwrite Not for 0, And the original content of the environment variable , The original content will be changed to parameters value The content of the indicated variable ; If overwrite by 0, And the environment variable already has content , Then parameter value Will be ignored ;

【 error code 】:ENOMEM, insufficient memory , Unable to configure new environment variable space ;

【 Return value 】: Successful execution return 0, Return when an error occurs -1;

【 Model example 】:

#include

#include

int main(void)

{

int ret;

char *p;

p = getenv("PATH");

if(p == NULL)

{

printf("Getenv failed.\n");

return -1;

}

printf("PATH=%s\n",p);

ret = setenv("PATH","/bin/:/sbin/:/user/sbin/:/user/bin/:/opt/cst_app/bin/",1);

if(ret < 0)

{

printf("Setenv failed.\n”");

return -1;

}

printf("Setenv OK.\n");

p = getenv("PATH");

if(p == NULL)

{

printf("Getenv failed.\n");

return -1;

}

printf("PATH=%s\n”",p);

return 0;

}

6.4 Delete an environment variable :unsetenv()

【 Function definition 】:char *unsetenv(const char *name);

【 Function description 】:unsetenv() Used to delete name Defined environment variables , Even if it doesn't exist, it's not an error ;

【 Parameter description 】:name Is the name of the environment variable to delete ;

【 Return value 】: Successful execution return 0, Return when an error occurs -1;

【 Model example 】:

#include

#include

int main(void)

{

char *p;

p = getenv("PATH");

if(p == NULL)

{

printf("Get env failed.\n");

return -1;

}

printf("PATH=%s\n",p);

unsetenv("PATH");

p = getenv("PATH");

if(p == NULL)

{

printf("Get env failed.\n");

return -1;

}

printf("PATH=%s\n",p);

return 0;

}

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform