CentOS Setup System Environment variables

Source: Internet
Author: User
Tags chmod centos

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

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.