Save the environment variables from the export

Source: Internet
Author: User
Article Title: save the environment variables from the export. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

When I was doing LFS recently, I found that the environment variables from the original export disappeared after the shell was restarted. After checking the environment variables on the internet, I realized that the export only temporarily stored the newly added environment variables in the memory, restart or start a new shell session. The solution is as follows:

1. Modify/etc/profile

After the restart, the environment variable is still there. However, this is not recommended because this setting will take effect for the shell of all users and affect system security.

2. Modify ~ /. Bashrc File

In addition, I have also referred to two articles that are useful. The following is an excerpt:

Customized Linux environment variables

Summary

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. (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. 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

/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

History Size = 1000

...

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'

COLORS =/etc/DIR_COLORS.xterm

COLUMNS = 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 exists.

TEST = Test...

$ Unset $ TEST # Delete the environment variable TEST

$ Env | grep TEST # This command is not output, proving that the environment variable TEST already exists

. 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 # 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

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.

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.