Introduction to Linux Environment variables

Source: Internet
Author: User
Tags echo command

1. Types of variables for Linux

According to the lifetime of the variables, the Linux variables can be divided into two categories:

(1) Permanent: Need to modify the configuration file, the variable is permanently in effect.

(2) Temporary: Using the Export command declaration, the variable will fail when the shell is closed.

2. Three ways to set variables

(1) Add variable "to all users (permanent)" in the/etc/profile file

Using VI to add a variable to the file/etc/profile file, the variable will be valid for all users under Linux and is "permanent".

Example: Edit the/etc/profile file, add the CLASSPATH variable

# Vi/etc/profile

Add the following content:

Export classpath=./java_home/lib; $JAVA _home/jre/lib

Note: If you want to change the file, you need to run it immediately.

# Source/etc/profile

Otherwise, it will only take effect the next time the user is re-entered.

(2) Add variable "to single user (permanent)" in. bash_profile file in User directory

Use VI to add variables to the. bash_profile file in the user directory, and change the amount to be valid only for the current user and be "permanent".

For example: Edit the. Bash_profile under the Rachy User directory (/home/rachy)

$ vi/home/rachy/.bash.profile

Add the following content:

Export classpath=./java_home/lib; $JAVA _home/jre/lib

Note: If you want to change the file, you need to run it immediately.

$ source/home/rachy/.bash_profile

Otherwise, it will only take effect the next time the user is re-entered.

(3) Run the Export command directly to define the variable "valid only for the current shell (BASH)"

The variable is defined directly under the Shell's command line using the [export variable name = variable value], which is valid only under the current shell (bash) or its child shell (bash), the shell is closed, the variable is invalidated, and the new shell is opened without this variable. You need to redefine the words you need to use.

3. View of environment variables

(1) Use the echo command to view a single environment variable. For example:

Echo $PATH

(2) Use ENV to view all environment variables. For example:

Env

(3) Use Set to view all locally defined environment variables. For example:

Set

Use unset to delete the specified environment variable. For example:

Uset$test

4. Common environment variables

path determines to which directories the shell will look for commands or programs

Home Current User Home directory

Histsize Number of historical records

LogName The current user's login name

Hostname refers to the name of the host

Shell Current User shell type

Languge language-related environment variables, multiple languages can modify this environment variable

Mail storage directory for the current user of mail

PS1 basic prompt, for root user Yes #, for normal user is $

Instance:

1. Set a new environment variable test

$ export test= "TEST ..." #增加一个环境变量TEST

2. Display the environment variable test

$ echo $TEST

3. Use the ENV command to display all environment variables

$ env

4. Use the SET command to display all locally defined shell variables

$ set

5. Use the unset command to clear environment variables

$ unset $TEST #删除环境变量TEST

6. Set read-only variables using the readonly command

$ export test= "TEST ..." #增加一个环境变量TEST

$ readonly TEST #将环境变量TEST设为只读

$ unset TEST #会发现此变量不能被删除

$ test= "NewValue" #会发现此也变量不能被修改

The configuration file for the environment variable is/etc/profile, and if you need to add a new environment variable, one row can be added, such as:

Export path= $path:/PATH1:/PATH2:/PAHTN

Reference: http://soft.chinabyte.com/os/169/11412169.shtml


Introduction to Linux Environment variables

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.