1) The naïve practice of the first attempt:
Take the registered Golang environment variable as an example:
CD/ETC/PROFILE.D
Ls-rtl
Touch custom.sh # or any file name you like to create a new file
Ls-rtl
echo "E Xport Path=\${path}:/usr/local/go/bin "> custom.sh && chmod 755 custom.sh less
custom.sh
. custom.sh
Echo $PATH
reboot
It is not recommended to write directly to/etc/profile, but create a new shell script of your own, under the/ETC/PROFILE.D folder, which reads:
Export Path=${path}:/usr/local/go/bin
System startup automatically executes this script
For details, see the contents of the/etc/profile text:
#/etc/profile # System wide environment and startup programs, for login Setup # Functions and aliases Go IN/ETC/BASHRC # It's not a good the idea to the change of this file unless you know and is doing. It's much better to create a custom.sh shell script on #/etc/profile.d/to make custom changes to your environment, as th
is # would prevent the need for merging on future updates. Pathmunge () { case ': ${path}: ' in *: ' $ ':*)
; *) IF ["$" = "after"];
Then path= $PATH: $ Else path=$1: $PATH fi ESAC} ... for i in/etc/profile.d/*.sh; do if [-R "$i"]; Then if ["${-#*i}"! = "$-"]; Then.
"$i" else. "$i" >/dev/null fi fi Done
$-represents the options for the current shell, see 1 2 3
echo "$-" shows the Himbh (5 options)
${-#*i} * Here is the string wildcard, #这里是字符串操作符, ${string#match_string}
Deletes shortest match of $match _string from front of $string. See 4
So ${-#*i} is MBH
However, after the above approach, there are two/usr/local/go/bin in the Echo $PATH in terminal. It's harmless, but it's strange that the script has been executed two times.
Yes, it was executed two times:
~/.BASHRC---/ETC/BASHRC also executed it again.
2) The appropriate practice:
Remove duplicates using the Pathmunge function defined in/etc/profile (/ETC/BASHRC):
echo "Pathmunge/usr/local/go/bin after" > custom.sh && chmod 755 custom.sh
5