Experimental environment
- Alibaba cloud server
- Linux centos7
- Applicable to all Linux distributions (CentOS, Ubuntu, etc.) and embedded Linux
principle
environment variable
The export command can display all current environment variables
[root@shuaifriend ~]# export declare -x HISTCONTROL="ignoredups" declare -x HISTSIZE="1000" declare -x HOME="/root" declare -x HOSTNAME="shuaifriend" declare -x LANG="en_US.UTF-8" declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s" declare -x LOGNAME="root" declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:" declare -x MAIL="/var/spool/mail/root" declare -x OLDPWD declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/rabbitmq_server-3.8.3/sbin:/usr/local/node-v13.9.0-linux-x64/bin:/usr/local/redis-5.0.3/src:/opt/metasploit-framework/bin:/root/bin" declare -x PWD="/root" declare -x SHELL="/bin/bash" declare -x SHLVL="1" declare -x SSH_CLIENT="101.245.228.162 59318 22" declare -x SSH_CONNECTION="101.245.228.162 59318 172.19.35.156 22" declare -x SSH_TTY="/dev/pts/1" declare -x TERM="xterm" declare -x USER="root" declare -x XDG_RUNTIME_DIR="/run/user/0" declare -x XDG_SESSION_ID="2551"
Among them, we often use the environment variable PATH
The PATH variable defines the search PATH for running the command, which is divided into different paths by colons. It not only determines which directories the shell will search for commands or programs, but also determines the search PATH when compiling links.
View environment variables
export | grep PATH
env | grep PATH
echo $PATH
[root@shuaifriend ~]# export | grep PATH declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/rabbitmq_server-3.8.3/sbin:/usr/local/node-v13.9.0-linux-x64/bin:/usr/local/redis-5.0.3/src:/opt/metasploit-framework/bin:/root/bin" [root@shuaifriend ~]# [root@shuaifriend ~]# [root@shuaifriend ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/rabbitmq_server-3.8.3/sbin:/usr/local/node-v13.9.0-linux-x64/bin:/usr/local/redis-5.0.3/src:/opt/metasploit-framework/bin:/root/bin
Modify environment variables (temporarily effective)
The method in this section only takes effect temporarily, and takes effect at the current terminal. Restart and recover
add to
export PATH=$PATH:/usr/local/xxxxxx/xxx
The previous instruction adds the value of PATH to the previous instruction, because the previous instruction adds the value of PATH to the previous instruction, and the latter takes the value of PATH.
delete
Violent way
Directly overwrite the PATH variable. You can use echo $PATH to view the current environment variable first
export PATH=New path (remove the path to be deleted)
This method is simple and crude. It is applicable to the test of temporarily deleting environment variables. It is only effective on the current terminal, and the restart will still be restored
Elegant way (using pipe symbols | and sed)
export PATH=`echo $PATH | sed -e 's/:\/usr\/local\/abc//g'`
export PATH=`echo $PATH | sed -e "s/:\/usr\/local\/abc//g"`
sed -e 's/aaa/bbb/g'
Replace aaa with bbb. If bbb is empty, it will be deleted. To translate / with \, / usr/local/abc needs to be written as \ / usr\/local\/abc. Note: and /, you'd better echo $PATH first, check the environment variable, determine the string to search, and determine the boundary of: and / or
For example:
sed -e 's/:\/usr\/local\/abc//g'
sed -e 's/\/usr\/local\/abc://g'
sed -e 's/\/usr\/local\/abc\/://g'
etc.
Examples
book@100ask:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/book/imx6ull/100ask/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin book@100ask:~$ export PATH=$PATH:/usr/local/rabbitmq/sbin book@100ask:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/book/imx6ull/100ask/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin:/usr/local/rabbitmq/sbin book@100ask:~$ export PATH=`echo $PATH | sed -e 's/:\/usr\/local\/rabbitmq\/sbin//g'` book@100ask:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/book/imx6ull/100ask/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin book@100ask:~$
Modify environment variables (permanent)
This section modifies the configuration file in such a way that the environment variables will take effect permanently.
Modify / ect/profine configuration file
This file is the system global configuration file. After modification, it acts on the environment variables of all users in the system
add to
echo 'export PATH=$PATH:/usr/local/rabbitmq/sbin' >> /etc/profile
delete
echo 'export PATH=`echo $PATH | sed -e "s/:\/usr\/local\/abc//g"`' >> /etc/profile
Modify ~ / bashrc configuration file
This file is the current user configuration file. After modification, it acts on the environment variables of the current user in the system
add to
echo 'export PATH=$PATH:/usr/local/rabbitmq/sbin' >> ~/.bashrc
delete
echo 'export PATH=`echo $PATH | sed -e "s/:\/usr\/local\/abc//g"`' >> ~/.bashrc
matters needing attention
The system loads / ect/profine first and then ~ / bashrc, to delete the environment variable is to pay attention to the loading order.