Set and view environment variables in Linux

Source: Internet
Author: User

Set and view environment variables in Linux
Linux variable types

Linux variables are divided by the life cycle of variables. There are two types of Linux variables:
1 permanent: the configuration file needs to be modified, and the variable takes effect permanently.
2. Temporary: Use the export command to declare the variable. The variable becomes invalid when the shell is closed.

Three methods for setting Variables

1. Add the variable in the/etc/profile file [effective for all users (permanent )]
Use VI to add a variable to the/etc/profile file. This variable will be valid for all users in Linux and will be "permanent ".
For example, edit the/etc/profile file and add the CLASSPATH variable.
# Vi/etc/profile
Export CLASSPATH =./JAVA_HOME/lib; $ JAVA_HOME/jre/lib

Note: to change the file to take effect immediately, run # source/etc/profile. Otherwise, it will only take effect the next time you re-enter the user.

2. Add a variable to the. bash_profile file in the user directory [effective for a single user (permanent )]
Use VI to add a variable to the. bash_profile file in the user directory. The change volume is valid only for the current user and is "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: to change the file to take effect immediately, run $ source/home/guok/. bash_profile. Otherwise, it will only take effect the next time you re-enter the user.

3 directly run the export command to define the variable [only valid for the current shell (BASH) (Temporary )]
Use the [export variable name = variable value] to define the variable under the shell command line,

This variable is valid only in the current shell (BASH) or its subshell (BASH,

When shell is closed, the variable becomes invalid. This variable is not available when a new shell is opened. You need to define the variable again if necessary.

View environment variables

1. Run 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.

Use unset to delete specified 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 has been deleted

Common Environment Variables

PATH determines the directories to which shell will look for commands or programs.
HOME main directory of the current user
HISTSIZE
LOGNAME: The Login Name of the current user.
HOSTNAME indicates the host name.
SHELL Current User Shell Type
LANGUGE language-related environment variables, which can be modified in multiple languages
MAIL current user's MAIL storage directory
PS1 basic prompt, for root users is #, for common users is $

This article permanently updates the link address:

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.