Linux environment variables

Source: Internet
Author: User

Linux environment variable Linux is a multi-user operating system. Each user logs on to the system and has a dedicated runtime environment. Generally, the default environment of each user is the same. The default environment is actually the definition of a set of environment variables. You can customize your running environment by modifying the corresponding system environment variables.
Common Environment Variables
You are familiar with environment variables such as path and home. In addition, there are some common environment variables.
◆ Histsize refers to the number of historical Command records.
◆ LOGNAME indicates the login name of the current user.
◆ Hostname refers to the host name, which is usually obtained from this environment variable if many applications use the host name.
◆ Shell refers to the shell used by the current user.
◆ Lang/Languge is a language-related environment variable, which can be modified by users in multiple languages.
◆ Mail refers to the mail storage directory of the current user.
◆ PS1 is the basic prompt, Which is # for root users and $ for common users. PS2 is an auxiliary prompt. The default prompt is "> ". You can modify this environment variable to modify the current command line. For example, the following command will change the prompt to the string "Hello, my newprompt ".
# PS1 = "Hello, my newprompt" Hello, my newprompt

In addition to these common environment variables, many applications also add some environment variables during installation. For example, to use Java, you must set java_home and classpath, when the five input methods are installed, the environment variable "xmodifiers = @ im = fcitx" is added.
Customized Environment Variables


Environment variables are closely related to shell. A shell is started after the user logs on to the system. For Linux, bash is generally used, but you can reset or switch to another shell. Environment variables are set through shell commands. The configured environment variables can be used by all programs run by the current user. For the bash shell program, the corresponding environment variables can be accessed through the variable name, and the environment variables can be set through export. The following describes several instances.
1. display the environment variable home
$ Echo $ home/Terry
2. Set a new environment variable welcome.
$ Export welcome = "Hello! "$ Echo $ welcomehello!
3. Use the Env command to display all environment variables
$ Envhostname = Terry. mykms. orgpvm_rsh =/usr/bin/rshshell =/bin/bashterm = xtermhistsize = 1000...
4. Use the SET command to display all locally defined shell Variables
$ Setbash =/bin/bashbash_versinfo = ([0] = "2" [1] = "05b" [2] = "0" [3] = "1" [4] = "release" [5] = "i386-redhat-linux-gnu ") bash_version = '2. 05b. 0 (1)-release 'colors =/etc/dir_colors.xtermcolumns = 80 dirstack = () display =: 0. 0...
5. Run the unset command to clear environment variables.
Set can be used to set the value of an environment variable. Run the unset command to clear the environment variable values. If no value is specified, the variable value is set to null. Example:
$ Export test = "test... "# Add an environment variable test $ ENV | grep test # This command is input to prove that the environment variable test already has test = test... $ unset $ test # Delete the environment variable test $ ENV | grep test # This command has no output, proving that the environment variable test already exists.
6. Use the readonly command to set the read-only variable
If the readonly command is used, the variables cannot be modified or cleared. Example:
$ Export test = "test... "# Add an environment variable test $ readonly test # Set the environment variable test as read-only $ unset test # You will find that this variable cannot be deleted-Bash: unset: Test: cannot unset: readonly variable $ test = "new" # This variable cannot be modified-Bash: Test: readonly variable
7. Use a C program to access and set Environment Variables
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 () is a function used to set an environment variable in a 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 <stdio. h> extern char ** environ; int main () {char ** var; For (Var = environ; * var! = NULL; ++ var) printf ("% s/n", * var); Return 0 ;}

You can also modify the environment variables by modifying some related environment definition files, for example, for Linux releases such as red hat, environment-related files include/etc/profile and ~ /. Bashrc. After modification, you can log on again.
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.