How to set and view Linux environment variables

Source: Internet
Author: User
Tags echo command

1. Display Environment Variables Home

$ echo $HOME

/home/redbooks

2. Set a new environment variable Hello

$ export hello= "hello!"

$ echo $HELLO

Hello!

3. Use the ENV command to display all environment variables

$ env

hostname=redbooks.safe.org

Pvm_rsh=/usr/bin/rsh

Shell=/bin/bash

Term=xterm

histsize=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. Use the unset command to clear environment variables

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

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

$ env|grep Test #此命令有输入, proving that the environment variable test already exists

Test=test ...

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

$ env|grep Test #此命令没有输出, proving that the environment variable test already exists

6. Set read-only variables using the readonly command

If the readonly command is used, the variable cannot be modified or erased. Examples are as follows:

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

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

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

-bash:unset:test:cannot unset:readonly Variable

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

-bash:test:readonly variable

Settings for environment variables are located in the/etc/profile file

If you need to add a new environment variable, you can include a subordinate row

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

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

Variable type of 1.Linux

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

1.1 Permanent: The configuration file needs to be modified, and the variable will take effect permanently.

1.2 Temporary: With the Export command declaration, the variable is invalidated when the shell is closed.

2. Three ways to set variables

2.1 Adding variables to the/etc/profile file "in effect for all users (permanent)"

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

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

Note: If you want to run the file immediately after you modify it, you can only take effect the next time you re-enter the user. Source/etc/profile.

2.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 Guok User directory (/home/guok)

$ vi/home/guok/.bash.profile

Add the following content:

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

Note: If you want to take effect immediately after modifying a file, run $ source/home/guok/.bash_profile or you can only take effect the next time you re-enter the user.

2.3 Run the Export command directly 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

3.1 Use the echo command to view a single environment variable. For example:

Echo $PATH

3.2 Use Env to view all environment variables. For example:

Env

3.3 Use Set to view all locally defined environment variables.

Unset can delete the specified environment variable.

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 $

How to set and view Linux environment variables

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.