Modify system environment variables in Linux

Source: Internet
Author: User

1. Summary background

In Linux, If you download and install an application, it is very likely that the prompt "command not found" appears when you type its name. If you find the executable file in the installation target folder every time, it is too cumbersome to find the executable file. This involves setting the environment variable path, and the path setting is also an integral part of customized environment variables in Linux. This case explains how to customize environment variables based on Redhat as4.

2. Introduction to variables

Linux is a multi-user operating system. Each user logs on to the system and has a dedicated runtime environment. Generally, the default environment of each user is the same. The default environment is actually the definition of a set of environment variables. You can customize your running environment by modifying the corresponding system environment variables.

3. Customize Environment Variables

Environment variables are closely related to shell. A shell is started after the user logs on to the system. It is usually bash for Linux, but it can also be reset or switched to another shell (using the CHSH command ).

Based on the release version, Bash has two basic system-level configuration files:/etc/bashrc and/etc/profile. These configuration files contain two different variables: Shell variables and environment variables. The former is only fixed in a specific shell (such as Bash), and the latter is fixed in different shells. Obviously, shell variables are local, while environment variables are global. Environment variables are set through shell commands. The configured environment variables can be used by all programs run by the current user. For the bash shell program, the corresponding environment variables can be accessed through the variable name, and the environment variables can be set through export.

Note: Linux environment variable names generally use uppercase letters

 

4. Set environment variables for instances

1. Use the echo command to display environment variables

In this example, ECHO is used to display common variables home.

$ Echo $ home

/Home/Kevin

2. Set a new environment variable

$ Export myname = "My name is Kevin"

$ Echo $ myname

My name is Kevin

3. Modify existing environment variables

Example

$ Myname = "change name to Jack"

$ Echo $ myname

Change name to Jack

4. Use the Env command to display all environment variables

$ ENV

Hostname = localhost. localdomain

Shell =/bin/bash

Term = xterm

History Size = 1000

Ssh_client = 192.168.136.151 1740 22

Qtdir =/usr/lib/qt-3.1

Ssh_tty =/dev/pts/0

......

5. Use the SET command to display all locally defined shell Variables

$ Set

Bash =/bin/bash

Bash_env =/root/. bashrc

......

6. Run the unset command to clear environment variables.

$ Export temp_kevin = "Kevin" # Add an environment variable temp_kevin

$ ENV | grep temp_kevin # Check whether the environment variable temp_kevin takes effect (if any)

Temp_kevin = Kevin # verify that the environment variable temp_kevin already exists

$ Unset temp_kevin # Delete the environment variable temp_kevin

$ ENV | grep temp_kevin # Check whether the environment variable temp_kevin has been deleted. If no output is displayed, it indicates that temp_kevin has been cleared.

7. Use the readonly command to set the read-only variable

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

$ Export temp_kevin = "Kevin" # Add an environment variable temp_kevin

$ Readonly temp_kevin # Set the environment variable temp_kevin to read-only

$ ENV | grep temp_kevin # Check whether the environment variable temp_kevin takes effect

Temp_kevin = Kevin # verify that the environment variable temp_kevin already exists

$ Unset temp_kevin # The system will prompt that this variable cannot be deleted only.

-Bash: unset: temp_kevin: cannot unset: readonly variable

$ Temp_kevin = "Tom" # Changing the variable value to Tom will prompt that the variable read-only cannot be modified.

-Bash: temp_kevin: readonly variable

8. Modify the environment variable definition file.

In general, only the environment variable configuration file of the common user is modified to avoid modifying the environment definition file of the root user, which may cause potential risks.

$ Cd ~ # Go to the user root directory

$ LS-A # view all files, including hidden files

$ VI. bash_profile # modify the user environment variable File

For example:

Edit your path declaration in the following format:

Path = $ path: <Path 1 >:< Path 2 >:< Path 3 >:------: <path n>

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

After the environment variable is changed, it will take effect the next time the user logs in.

To take effect immediately, run the following statement: $ source. bash_profile

Every user in the DB2 database has their own environment variables after login. In/home/yidukongjian, for example, yidukongjian is a user of mine, so there is a directory under it. bash_profile's environment variable file will overwrite the global environment variables we set in the/home directory. Therefore, if we want to use local environment variables, we need to modify them under this user. the bash_profile file is changed to VI. bash_profile, followed by source. bash_profile

Enter the source. bash_profile command to initialize your environment variables every time you open the terminal window.

Note that it is best not to put the current path "./" in the path, which may be subject to unexpected attacks.

After that, you can view the current search path through $ echo $ path. In this way, you can avoid frequent startup of programs outside the shell search path.

5. Learning Summary

1. 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 line to declare the variable. The variable becomes invalid when the shell is closed.

2. 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 the variable [effective for a single user (permanent)] To the. bash_profile file in the user directory )]

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. Run the Export command directly to define the variable [only valid for the current shell (BASH) (Temporary )]

Use the [Export variable name = variable value] to define a 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.

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.