Set environment variables in Linux

Source: Internet
Author: User


Linux environment variable 1. Environment variable is a string containing environment information about the system and the currently logged on user, some software programs use this information to determine where to place files (such as temporary files ). To set environment variables, you can specify a software path. An environment variable is a string that contains a drive, path, or file name. Environment variables control the behavior of multiple programs. For example, the TEMP environment variable specifies the location where the program places temporary files. Path indicates the system. When the system is required to run a program without telling it the complete path of the program, the system searches for this program under the current directory, you should also go to those directories to find www.2cto.com [permission questions] any user can add, modify or delete the user's environment variables. However, only Administrators can add, modify, or delete system environment variables. Linux is a multi-user operating system. Each user logs on to the system and has a dedicated runtime environment. To make each user's default environment the same, you need to set a set of environment variables. You can customize your runtime environment by modifying the corresponding system environment variables. Note: Linux environment variable names generally use uppercase letters. II. Environment variable settings 1. show environment variables this example uses echo to show common variables HOME $ echo $ HOME/home/kevin 2. set the new environment variable $ export MYNAME = "my name is kevin" www.2cto.com $ echo $ MYNAME my name is Kevin 3. example of modifying an existing environment variable to connect $ MYNAME = "change name to jack" $ echo $ MYNAME change name to jack 4. show all environment variables $ env www.2cto.com 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 $ set BASH =/bin/bash BASH_ENV =/root/. bashrc ...... Www.2cto.com 6. run the unset command to clear the environment variable $ export TEMP_KEVIN = "kevin" # Add an environment variable TEMP_KEVIN $ env | grep TEMP_KEVIN # Check whether the environment variable TEMP_KEVIN takes effect (if it exists, it takes effect) TEMP_KEVIN = kevin # prove 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 is deleted and no output is displayed, it proves 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 variable 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 # it proves that the environment variable TEMP_KEVIN already exists at www.2cto.com $ unset TEMP_KEVIN # will prompt that the variable read-only cannot be deleted-bash: unset: TEMP_KEVIN: cannot unset: readonly variable $ TEMP_KEVIN = "tom" # modifying the variable value to tom will prompt that the variable read-only cannot be modified-bash: TEMP_KEVIN: readonly variable 8. modify the environment variables by modifying 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 $ ls-a in the user root directory # view all files, including hidden files $ vi. bash_profile # modify the user environment variable file. For example, edit your PATH declaration in the format of www.2cto.com PATH = $ PATH: <PATH 1 >:< PATH 2 >:< PATH 3>: ------: <path n> you can add the specified PATH, 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 should note that it is best not to set the current path ". /"put 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. Iii. Summary commonly used: env displays all environment variables

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.