LINUX environment variable Summary __linux

Source: Internet
Author: User

When we install the system software under Linux, we often encounter some problems of system environment variable configuration. What is an environment variable. How to customize environment variables. I'll do some introductions below.

First, what is the environment variable.
Linux is a multi-user operating system. Multi-user means that each user has its own dedicated operating environment after logging on to the system. This environment is defined by a set of variables called environment variables. Users can modify their environment variables to meet the requirements of the environment.

Second, the custom environment variable
Environment variables are closely related to the shell and are set by the shell command. Environment variables can also be used by programs that are run by all current users. For bash, the corresponding environment variables can be accessed through variable names.
Here are a few examples to illustrate

1. Show Environment Variables Home
$ echo $HOME
/home/admin

2. Set a new environment variable name
$ export Name= "Raidcheng"
$ echo $NAME
Raidcheng

3. Display all environment variables using the ENV command
$ env
Hostname=test
term=vt100
Shell=/bin/bash
histsize=1000
Ssh_client=202.xxx.xxx.xxx 53694 22
Catalina_base=/usr/local/jakarta-tomcat
ssh_tty=/dev/pts/0
Ant_home=/usr/local/ant
Java_opts=-server
User=admin
...

4. Display all locally defined shell variables using the SET command
$ set
Bash=/bin/bash
bash_versinfo= ([0]= "2" [1]= "05b" [2]= "0" [3]= "1" [4]= "Release" [5]= "I386-redhat-linux-gnu")
Bash_version= ' 2.05b.0 (1)-release '
Catalina_base=/usr/local/jakarta-tomcat
Catalina_home=/usr/local/jakarta-tomcat
...

5. Use the unset command to clear environment variables
$ export Name= "Raidcheng"
$ echo $NAME
Raidcheng
$ unset NAME
$ echo $NAME

6. Set read-only variables using the readonly command
$ export Name= "Raidcheng"
$ readonly NAME
$ unset NAME
-bash:unset:name:cannot unset:readonly Variable
$ name= "New" #会发现此也变量不能被修改
-bash:test:readonly variable

III. Common Environment variables
PATH determines which directories the shell will go to find commands or programs
Home Current User main directory
Histsize Number of history records
LOGNAME The current user's login name
HOSTNAME refers to the name of the host
Shell Front user shell type
Languge language-related environment variables, multiple languages can modify this environment variable
Mail storage directory for current user
PS1 base prompt, for Root is #, for average user is $
PS2 secondary prompt, default is ">"

Iv. Accessing and setting environment variables through C programs
For users of the C program, you can use the following three functions to set or access an environment variable.

Getenv () accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string. If the environment variable being accessed does not exist, a null is returned

Setenv () a function that sets an environment variable inside a program

Unsetenv () Clears a function of a particular environment variable

In addition, there is a pointer variable environ, which points to a list of all the environment variables. The following program can print out 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/n", *var);
return 0;
}

V. Environment variable Documents
Modify environment variables by modifying some relevant environmental definition files, such as for Redhat, environment-related documents such as/etc/profile and ~/.bash_profile. Once the modification is complete, log in again or Run command source XXX to take effect.

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.