Linux environment variable

xiaoxiao2021-03-06  48

Turn: LinuxAid

Summary

Linux is a multi-user operating system. After each user logs in to the system, there will be a dedicated operational environment. Usually, each user's default environment is the same, this default environment is actually a definition of a set of environment variables. Users can customize their own running environment, which is to modify the corresponding system environment variable. (2004-08-24 20:31:44)

-------------------------------------------------- ------------------------------

By lanf, source: http://tech.ccidnet.com/pub/Article/c309_a100764_p1.html

Author: Yu Haifa

Source: Open System World

Linux is a multi-user operating system. After each user logs in to the system, there will be a dedicated operational environment. Usually, each user's default environment is the same, this default environment is actually a definition of a set of environment variables. Users can customize their own running environment, which is to modify the corresponding system environment variable.

Common environment variables

Everyone is unfamiliar with environment variables such as Path and HOME. In addition, there are some common environment variables below.

◆ HISTSIZE means the number of keys to save history commands.

◆ logname refers to the login name of the current user.

◆ Hostname refers to the name of the host. Many applications are usually achieved from this environment variable if they want to use the host name.

◆ Shell means which shell is currently used.

◆ Lang / LANGUGE is a language-related environment variable, and users who use multiple languages ​​can modify this environment variable.

◆ mail refers to the current user's mail storage directory.

◆ PS1 is a basic prompt, for the root user is #, for ordinary users is $. PS2 is a subsidiary prompt, default ">". You can modify the current command by modifying this environment variable, such as the following command modifies the prompt "Hello, My NewPROMPT :).

# Ps1 = "Hello, my newprompt :)"

Hello, my newprompt :)

In addition to these common environment variables, many applications will also add some environment variables when installing. For example, Java_Home and ClassPath are set, and the five-input Fa will increase the environment variable "XModifiers = @ im = fcitx".

Custom environment variable

The environment variable is closely related to Shell, and then the user launched a shell after logging in to the system. For Linux, it is generally BASH, but it can also be reset or switched to other shells. The environment variable is set by the shell command, and the set environment variable can be used by the program running by all current users. For Bash this shell program, you can access the corresponding environment variables through the variable name, and set the environment variables via export. The following is a few examples.

1. Display environment variable Home

$ Echo $ HOME

/ home / terry

2. Set a new environment variable Welcome

$ export welcome = "Hello!"

$ Echo $ Welcome

Hello!

3. Use the eNV command to display all environment variables

$ ENV

Hostname = Terry.mykms.org

PVM_RSH = / usr / bin / rsh

Shell = / bin / bash

Term = xterm

Histsize = 1000

...

4. Use the set command to display all local 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'

Colors = / etc / DIR_COLORS.XTERM

Column = 80

Dirstack = ()

DISPLAY =: 0.0

...

5. Use the unset command to clear the environment variable

SET can set the value of an environment variable. Clear the value of the environment variable with the unset command. If no value is specified, the variable value will be set to NULL. Examples are as follows:

$ export test = "test ..." # Add an environment variable TEST

$ ENV | grep test # This command has input, which proves that the environment variable TEST already exists.

Test = Test ...

$ unset $ test # Delete Environment Variable Test

$ env | grep test # This command has no output, which proves that the environment variable TEST already exists.

6. Use the readonly command to set read-only variables

If you use the readonly command, the variable cannot be modified or cleared. Examples are as follows:

$ export test = "test ..." # Add an environment variable TEST

$ readonly test # Set the environment variable TEST to read-only

$ unset test # will find that this variable cannot be deleted

-Bash: Unset: Test: Cannot Unset: Readonly Variable

$ Test = "new" # will find that this also variable cannot be modified

-Bash: Test: Readonly Variable

7. Use the C program to access and set environment variables

For users of the C program, you can use the following three functions to set or access an environment variable.

◆ GetENV () Access an environment variable. The input parameter is a variable name that needs to be accessed, and the return value is a string. If the accessed environment variable does not exist, NULL will be returned.

◆ STENV () Sets a function of an environment variable in the program.

◆ Unstenv () Clear a function of a particular environment variable.

In addition, there is a pointer variable Environment, which pointing is a list of all environment variables. The following program can print all 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;

}

You can also modify environment variables by modifying some related environment definition files, such as Linux release versions such as Red Hat, with / etc / profile and ~ / .bashrc, etc. After the modification is completed, you will take effect again.

转载请注明原文地址:https://www.9cbs.com/read-115993.html

New Post(0)