Summary of Linux environment variables ......

Source: Internet
Author: User

We often encounter system environment variable configuration problems when installing system software in Linux. What are environment variables? How to customize environment variables? I will introduce it below.

1. What are environment variables?
Linux is a multi-user operating system. Multiple users mean that each user has a dedicated runtime environment after logging on to the system. This environment is defined by a group of variables, which are called environment variables. You can modify your environment variables to meet the environment requirements.

Ii. customized Environment Variables
Environment variables are closely related to shell, which are set through shell commands. Environment variables can be used by all programs run by the current user. For Bash, you can use the variable name to access the corresponding environment variables.
Several examples are provided below.

1. display the environment variable home
$ Echo $ home
/Home/admin

2. Set a new environment variable name
$ Export name ="Raidcheng"
$ Echo $ name
Raidcheng

3. Use the Env command to display all environment variables
$ ENV
Hostname = test
Term = VT100
Shell =/bin/bash
History Size = 1000
Ssh_client = 202. 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. Use the SET command to display all locally defined shell Variables
$ 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. Run the unset command to clear environment variables.
$ Export name ="Raidcheng"
$ Echo $ name
Raidcheng
$ Unset name
$ Echo $ name

6. Use the readonly command to set the read-only variable
$ Export name ="Raidcheng"
$ Readonly name
$ Unset name
-Bash: unset: Name: cannot unset: readonly variable
$ Name ="New"# This variable cannot be modified.
-Bash: Test: readonly variable

Iii. Common Environment Variables
Path determines the directories to which shell will look for commands or programs.
Home main directory of the current user
Histsize
LOGNAME: The Login Name of the current user.
Hostname indicates the host name.
Shell Type before Shell
Languge language-related environment variables, which can be modified in multiple languages
Mail current user's mail storage directory
PS1 basic prompt, for root users is #, for common users is $
PS2 affiliated prompt. The default value is ">"

4. Access and set the environment change through the C program
For C program users, you can use the following three functions to set or access an environment variable.

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

Setenv (): set the number of letters of an environment variable in the program.

Unsetenv () is a function used to clear a specific environment variable.

In addition, the pointer variable environ points to a list containing all environment variables. The following program can print 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 files
Modify environment variables by modifying related environment definition files. For example, for Redhat, environment-related files include/etc/profile and ~ /. Bash_profile. After modification, log on again or run the source XXX command.

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.