How to set environment variables under Linux

Source: Internet
Author: User

Linux How to set environment variables, summed up today, issued to share with you, I hope that the learning of everyone to help, do not say, see how the specific operation of ...

1. Summary background

Under Linux, the application is downloaded and installed, and it is likely that the "command not found" prompt appears when you type its name. Each time you go to the Installation destination folder, it is cumbersome to find the executable file to operate. This involves setting the environment variable path, and the path setting is also an integral part of customizing environment variables under Linux. The case is based on Redhat AS4 to explain the customization of environment variables.

2. Introduction to Variables

Linux is a multi-user operating system. After each user logs on to the system, there will be a dedicated running environment. Typically, the default environment for each user is the same, and this default environment is actually the definition of a set of environment variables. Users can customize their running environment by modifying the corresponding system environment variables.

3. Custom Environment variables

Environment variables are closely related to the shell, and the user starts a shell after logging in to the system. For Linux it is generally bash, but you can also reset or switch to another shell (using the CHSH command).

According to the release version, Bash has two basic system-level profiles:/ETC/BASHRC and/etc/profile. These configuration files contain two different sets of variables: Shell variables and environment variables. The former is fixed in a particular shell (such as bash), and the latter is fixed in different shells. Obviously, the shell variable is local, and the environment variable is global. Environment variables are set through shell commands, and set up environment variables that can be used by programs run by all current users. For bash, the shell program can access the corresponding environment variables through the variable name, setting the environment variable by export.

Note: Linux environment variable names are typically used in uppercase letters

4. Environment variable Setting example

1. Display environment variables using command echo

This example uses ECHO to display common variables home

$ echo $HOME

/home/kevin

2. Set up a new environment variable

$ export Myname= "My name is Kevin"

$ echo $ myname

My name is Kevin

3. Modifying existing environment variables

Take the previous example

$ myname= "Change name to Jack"

$ echo $MYNAME

Change name to Jack

4. Display all environment variables using the ENV command

$ env

Hostname=localhost.localdomain

Shell=/bin/bash

Term=xterm

histsize=1000

ssh_client=192.168.136.151 1740 22

qtdir=/usr/lib/qt-3.1

ssh_tty=/dev/pts/0

......

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

$ set

Bash=/bin/bash

Bash_env=/root/.bashrc

......

6. Use the unset command to clear environment variables

$ export temp_kevin= "KEVIN" #增加一个环境变量TEMP_KEVIN

$ env | grep Temp_kevin #查看环境变量TEMP_KEVIN是否生效 (existing is in effect)

Temp_kevin=kevin #证明环境变量TEMP_KEVIN已经存在

$ unset Temp_kevin #删除环境变量TEMP_KEVIN

$ env | grep temp_kevin #查看环境变量TEMP_KEVIN是否被删除, no output display, proof Temp_kevin was cleared.

7. Set read-only variables using the readonly command

Note: If the readonly command is used, the variable cannot be modified or cleared.

$ Export Temp_kevin = "KEVIN" #增加一个环境变量TEMP_KEVIN

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

$ env | grep Temp_kevin #查看环境变量TEMP_KEVIN是否生效

Temp_kevin=kevin #证明环境变量TEMP_KEVIN已经存在

$ unset Temp_kevin #会提示此变量只读不能被删除

-bash:unset:temp_kevin:cannot unset:readonly Variable

$ temp_kevin = "Tom" #修改变量值为tom会提示此变量只读不能被修改

-bash:temp_kevin:readonly variable

8. Modify the environment variable by modifying the environment variable definition file.

It should be noted that, in general, only the normal user environment variable profile is modified to avoid modifying the root user's environment definition file, which can be potentially dangerous.

$ cd ~ #到用户根目录下

$ ls-a #查看所有文件, containing hidden files

$ VI. Bash_profile #修改用户环境变量文件

For example:

Edit your path declaration in the form of:

Path= $PATH::::------:

You can add the specified path yourself, separated by a colon in the middle.

When the environment variable is changed, it takes effect the next time the user logs on.

If you want to take effect immediately, you can execute the following statement: $source. bash_profile

It should be noted that it is best not to put the current path "./" Into the path, which may be subject to unexpected attacks.

When you are done, you can view the current search path through the $ echo $PATH. With this customization, you can avoid frequent startup of programs located outside the path of the shell search.

5. Learning Summary

Variable type of 1.Linux

According to the life cycle of variables, 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 line declaration, the variable fails when the shell is closed.

2. Three ways to set a variable

1. Add variable 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. Add 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.

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.

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.