Summary of Linux Set Environment Variables

Source: Internet
Author: User
Keywords linux set environment variable create environment variable linux linux set environment

Linux is a multi-user and multi-tasking operating system. You can set different operating environments for different users in Linux. The specific method is to set environment variables for different users.

Alibaba Cloud Simple Application Server:  Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.

Linux environment variable classification

According to the life cycle, Linux environment variables can be divided into two categories:
1. Permanent: The user is required to modify the relevant configuration files, and the variables take effect permanently.
2. Temporary: The user uses the export command to declare environment variables in the current terminal, and it is invalid to close the Shell terminal.

According to the scope, Linux environment variables can be divided into:
1. System environment variables: System environment variables are valid for all users in the system.
2. User environment variable: As the name implies, this type of environment variable is only valid for specific users.

How to set environment variables in Linux

1. Adding variables in the /etc/profile file is effective for all users (permanent)
Use vim to add a variable to the file /etc/profile. The variable will be valid for all users under Linux and is "permanent".
For example: edit /etc/profile file, add CLASSPATH variable
vim /etc/profile    
  export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib

Note: After modifying the file, you must run source /etc/profile to take effect immediately, otherwise it can only take effect the next time you re-enter this user.

2. Add variables to the .bash_profile file in the user directory [effective for a single user (permanent)]
Use vim ~/.bash_profile file to increase the variable, the change will only be effective for the current user, and it is "permanent".
vim ~/.bash.profile
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
Note: After modifying the file, you must run $ source ~/.bash_profile to take effect immediately, otherwise it can only take effect when you re-enter this user next time.

3. Run the export command directly to define variables [only valid for the current shell (BASH) (temporary)]

Use export variable name = variable value directly under the command line of the shell
Define a variable. The variable is valid only in the current shell (BASH) or its subshell (BASH). When the shell is closed, the variable is invalid. When you open a new shell, there is no such variable. If you need to use it, you need it. redefine.

Linux environment variable usage
1. Common environment variables in Linux are:
  • PATH: Specify the search path of the command
PATH statement usage:
PATH=$PAHT:<PATH 1>:<PATH 2>:<PATH 3>:--------:< PATH n>
export PATH
You can add the specified path by yourself, separated by a colon. After the environment variable is changed, it will take effect the next time the user logs in.
You can use echo $PATH to view the current system PATH path.

  • HOME: Specify the user's home working directory (that is, the default directory when the user logs in to the Linux system).
  • HISTSIZE: Refers to the number of historical command records saved.
  • LOGNAME: Refers to the login name of the current user.
  • HOSTNAME: Refers to the name of the host, if many applications use the host name, it is usually obtained from this environment variable
  • SHELL: Refers to what kind of Shell the current user uses.
  • LANG/LANGUGE: Language-related environment variables, users of multiple languages can modify this environment variable.
  • MAIL: Refers to the mail storage directory of the current user.
Note: The names of the above variables are not fixed. For example, HOSTNAME may be set to HOST in some Linux systems

2. Linux also provides commands to modify and view environment variables. Here are a few examples to illustrate:
  • echo displays the value of an environment variable echo $PATH
  • export set a new environment variable export HELLO="hello" (can be unquoted)
  • env shows all environment variables
  • set displays locally defined shell variables
  • unset clear environment variables unset HELLO
  • readonly set read-only environment variable readonly HELLO
3, C program calls environment variable functions
  • getenv() returns an environment variable.
  • setenv() sets an environment variable.
  • unsetenv() clears an environment variable.
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.