Linux environment variables

Source: Internet
Author: User

Environment Variable description

A. Environment Variables In Linux, If you download and install the applicationProgramThe 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 the setting of the Environment Variable path, which is also an integral part of customized environment variables in Linux.   B. 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.   C. Customize Environment Variables Environment variables are closely related to shell. After a user logs on to the system, a shell is started. 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. 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   D. Set the instance Environment Variables   A. production depends on Environment Variables For example, check the $ PATH environment variable. # Echo $ path Or # $ Path   B. Set a new environment variable. # Export myname = "My name is Kevin" # Echo $ myname My name is Kevin   C. modify an existing environment variable # Myname = "change name to Jack" # Echo $ myname Change name to Jack   D. 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 ......   E. Use the SET command to display all locally defined shell Variables $ Set Bash =/bin/bash Bash_env =/root/. bashrc ......   F. 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.   G. 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   H. Modify the environment variable definition file Note that only the environment variable configuration file of the common user is modified normally 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 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.   Learning Summary   A. Types of Linux Variables 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.   B. Three Methods for setting Variables   1. Add a variable to 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. 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.