Linux settings and ways to view environment variables

Source: Internet
Author: User
Tags echo command valid

1. Show Environment Variables Home

$ echo $HOME

/home/redbooks

2. Set up a new environment variable Hello

$ export hello= "hello!"

$ echo $HELLO

Hello!

3. Display all environment variables using the ENV command

$ env

hostname=redbooks.safe.org

Pvm_rsh=/usr/bin/rsh

Shell=/bin/bash

Term=xterm

histsize=1000

...

4. Display all locally defined shell variables using the SET command

$ 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 can set the value of an environment variable. Clears the value of an 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 #此命令有输入 that the environment variable test already exists

Test=test ...

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

$ env|grep Test #此命令没有输出 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 cleared. 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

Add subordinate rows if new environment variables need to be added

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

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

Variable type of 1.Linux

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

1.1 Permanent: Need to modify the configuration file, the variable is permanently in effect.

1.2 Temporary: Using the Export command declaration, the variable fails when the shell is closed.

2. Three ways to set a variable

2.1 Adding variables to/etc/profile file "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".

For example: Edit/etc/profile file, add classpath variable

# Vi/etc/profile

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

Note: If you modify the file, you want to take effect immediately and run # Source/etc/profile, otherwise it will only take effect the next time you go back to this user.

2.2 Adding a variable to the. bash_profile file in the user directory "for a single user (permanent)"

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

For example: Edit Guok User directory (/home/guok). Bash_profile

$ vi/home/guok/.bash.profile

Add the following:

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

Note: After modifying the file, you want to run the $ source/home/guok/.bash_profile before you can enter it again.

2.3 Direct run Export command definition variable "only valid (temporary) for current shell (BASH)"

Define variables directly under the command line of the shell using [export variable name = variable value]. The variable is valid only under the current shell (bash) or its child shell (bash), the shell is closed, the variable is invalidated, and when the new shell is opened, there is no such 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. Commonly used environment variables

PATH determines which directories the shell will go to find commands or programs

Home Current User main directory

Histsize Number of history 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 current user

PS1 base prompt, for Root is #, for average user is $

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.