View and add environment variables under Linux

Source: Internet
Author: User

Environment variables, in short, is to specify a directory, when running the software, the relevant program will follow the directory to find relevant files. The most practical function of setting a variable is that you don't have to copy some DLL files into the system directory, and path is a system variable that is a series of paths that the system searches for DLL files.

Under the Linux system, if you download and install the application, it is likely that the "command not found" prompt will appear when you type its name. If you go to the installation destination folder each time, it is too cumbersome to find the executable file to work on. This involves setting the environment variable path, and the path setting is also a component of customizing the environment variable under Linux. Linux viewing environment variables and setting environment variables are common in the use process, and this article organizes some common commands related to environment variables.

$PATH: determines which directories the shell will look for commands or programs, the value of PATH is a series of directories, and when you run a program, Linux searches for compiled links in these directories.

Edit your PATH declaration in the following format:

Path= $PATH: <path 1>:<path 2>:<path 3>:...:<path n>

You can add the specified path yourself, separated by a colon. When the environment variable is changed, it takes effect the next time the user logs on, and if you want to take effect immediately, you can execute the following statement: $ source. bash_profile

It is important to note that it is best not to put the current path "./" in path, which may be subject to unexpected attacks. When you are finished, you can view the current search path through the echo $PATH. With this customization, you can avoid frequent launches of programs that are outside the path of the shell search.

First, view environment variables

(1) Use the Export command to view the path value

1bogon:~a1$ Export2Declare-x home="/USERS/A1"3Declare-x logname="A1"4Declare-x mail="/VAR/MAIL/A1"5Declare-x objc_disable_initialize_fork_safety="YES"6DECLARE-x oldpwd7Declare-x path="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin"8Declare-x pwd="/USERS/A1"9Declare-x shell="/bin/bash"TenDeclare-x shlvl="1" OneDeclare-x ssh_client="10.144.33.72 60090" ADeclare-x ssh_connection="10.144.33.72 60090 10.144.24.130" -Declare-x ssh_tty="/dev/ttys001" -Declare-x term="xterm" theDeclare-x tmpdir="/var/folders/ny/n3gm1zvn43q_6cc900xdc59c0000gn/t/" -Declare-x user="A1"

(2) view PATH environment variables separately, available Echo $PATH

1 Echo $PATH 2 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin

(3) Display all environment variables with the ENV command

1bogon:~ a1$Env2term=xterm3shell=/bin/Bash4tmpdir=/var/folders/ny/n3gm1zvn43q_6cc900xdc59c0000gn/t/5Ssh_client=10.144.33.72 60090  A6ssh_tty=/dev/ttys0017objc_disable_initialize_fork_safety=YES8User=A19mail=/var/mail/A1Tenpath=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin Onepwd=/users/A1 AShlvl=1 -home=/users/A1 -Logname=A1 thessh_connection=10.144.33.72 60090 10.144.24.130  A -_=/usr/bin/Env

(4) Display all locally defined shell variables with the SET command

1bogon:~a1$ Set2bash=/bin/Bash3Bash_argc=()4bash_argv=()5bash_lineno=()6Bash_source=()7Bash_versinfo= ([0]="3"[1]="2"[2]=" $"[3]="1"[4]="Release"[5]="x86_64-apple-darwin17")8bash_version='3.2.57 (1)-release'9columns=236Tendirstack=() OneEuid=501 Agroups=() -histfile=/users/a1/. Bash_history -Histfilesize= - theHistsize= - -home=/users/A1 -Hostname=Bogon -Hosttype=x86_64 +ifs=$'\t\n' -lines= About +Logname=A1 Amachtype=x86_64-apple-darwin17 atmail=/var/mail/A1 -mailcheck= - -objc_disable_initialize_fork_safety=YES -Opterr=1 -Optind=1 -Ostype=darwin17 inpath=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/mysql/bin -Pipestatus= ([0]="0") toPpid=40148 +ps1='\h:\w \u\$' -Ps2='>' theps4='+ ' *pwd=/users/A1 $shell=/bin/BashPanax Notoginsengshellopts=braceexpand:emacs:hashall:histexpand:history:interactive-Comments:monitor -Shlvl=1 theSsh_client='10.144.33.72 60090' +ssh_connection='10.144.33.72 60090 10.144.24.130' Assh_tty=/dev/ttys001 theterm=xterm +tmpdir=/var/folders/ny/n3gm1zvn43q_6cc900xdc59c0000gn/t/ -Uid=501 $User=A1 $_=Env

Second, set environment variables

(1) Add PATH environment variable (temporary valid), run Export command to define variable directly at terminal

Define the variable directly under the shell's command line using the following format [export variable name = variable value]

1 [[email protected] u-boot-sh4] #export path=/opt/stm/stlinux-2.3/devkit/sh4/bin: $PATH 2 3 Check again: 4 5 Echo $PATH 6 /opt/stm/stlinux-2.3/devkit/sh4/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/ Sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Description Add path succeeded.

The path of the above method disappears after the terminal is closed.

(2) Add variable "to single user (permanent)" in. bash_profile file in User directory

Use VI to add variables to the. bash_profile file in the user directory, and change the amount to be valid only for the current user and be "permanent". This method is more secure, it can control the use of these environment variable permissions to the user level, here is for a specific user, if you need to give a user permissions to use these environment variables, you only need to modify their personal user home directory of the. bash_profile file on it. usually. Bash_profile and. BASHRC think the effect is the same, that is, adding environment variables here in. BASHRC is OK.

For example: Edit the. Bash_profile under the Guok User directory (/home/guok)

1 VI  /home/guok/23 export path= "/opt/stm/stlinux-2.3/devkit/sh4/bin: $PATH"

Or
#vim ~/.BASHRC
Export path= "/opt/stm/stlinux-2.3/devkit/sh4/bin: $PATH"

Note: If you want to take effect immediately after modifying a file, run $ source/home/guok/.bash_profile or you can only take effect the next time you re-enter the user.

(3) Add Variable "to all users (permanent)" in the/etc/profile file

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".
Example: Edit the/etc/profile file, add the CLASSPATH variable

1 VI  /etc/2

Note: If you want to run the file immediately after you modify it, you can only take effect the next time you re-enter the user. Source/etc/profile.

Second, the system start reading order

/ETC/PROFILE,/ETC/BASHRC is the system Global environment variable setting
~/.PROFILE,~/.BASHRC private environment variable settings in the user home directory
When a shell process is acquired while logged into the system, it has three steps to read the Environment profile:
1), first read the Global environment variable/etc/profile, and then read the additional set of documents according to its content, such as/ETC/PROFILE.D and/ETC/INPUTRC.
2), and then according to the different user account, go to its root directory read ~/.bash_profile, if this can not read ~/.bash_login, this also can not read to read ~/.profile, these three document settings are basically the same, read has a priority relationship. Where Unbutu default does not have a ~/.bash_profile file, you can create a new one.
3), finally in accordance with the user account read ~/.BASHRC.

View and add environment variables under Linux

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.