There are two common ways to modify environment variables:
- Temporary settings
For example, we have just installed the Golang to add the goroot to the environment variable: export PATH=$PATH:/usr/lib/go-1.9 . If the original environment variable is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin , the export environment variable after executing the command will become: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/lib/go-1.9 . All we do is export PATH=$PATH:/usr/lib/go-1.9 equivalent to:
$PATH = "/usr/local/sbin:/usr......"$GOROOT = "/usr/lib/go-1.9"$PATH = $PATH + $GOROOT
How to view modified environment variables with commands:
echo $PATHOrenv
- Permanent settings
Locate profile the file, and then edit it:
vi /etc/profile#添加以下内容export GOROOT=/usr/lib/go-1.9export GOBIN=$GOROOT/binexport GOAPTH=$GOROOT/srcexport GO_WORK_PATH=/home/workspace/go #自定义的工作空间#前面只是定义了变量,最后一句是关键export PATH=$PATH:$GOROOT:$GOBIN:$GOPATH:$GO_WORK_PATH
To take effect immediately, it is necessary to implement:
source /ect/profile
Restarting and reboot then echo $PATH discovering the environment variables was set before we restarted, and did not fail because of a reboot.
However , sometimes you will find that the environment variables and our settings are completely non-line after rebooting. This time, you need to find the .bashrc file:
vi ~/.bashrc#发现最下面有以下几行:export GOROOT=/home/libexport GOBIN=$GOROOT/binexport GOAPTH=$GOROOT/src
The original is because the settings in the .bashrc file "overwrite" our profile settings in the, OK, we now comment (delete) These lines, restart. The environment variables we set are finally in effect.