Ubuntu: correct way to add to PATH after installation of new software, .profile vs .bashrc vs /etc/profile

huangapple go评论91阅读模式
英文:

Ubuntu: correct way to add to PATH after installation of new software, .profile vs .bashrc vs /etc/profile

问题

我按照文档中的说明下载并安装了tar.gz文件:https://golang.org/doc/install

我编辑了我的.profile文件,添加了以下几行代码:

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

第一行是来自文档的,第二行和第三行是来自O'Reilly的《学习Go》一书。

在我安装Go的章节之后,我的书要我安装hey库来对网站进行负载测试。

(base) n@u-IdeaPad-3-15IIL05: hey https://www.golang.org

Command 'hey' not found, but can be installed with:

sudo snap install hey       # version 0.1.2, or
sudo snap install hey-mail  # version 1.2.0
sudo apt  install hey       # version 0.1.2-2

See 'snap info <snapname>' for additional versions.

(base) n@u-IdeaPad-3-15IIL05:/media/n/NTFSUbuWin/Projects/Golang$ source $HOME/.profile(base) n@u-IdeaPad-3-15IIL05:/media/n/NTFSUbuWin/Projects/Golang$ hey https://www.golang.org

Summary:
  Total:        1.5133 secs
  Slowest:      0.6518 secs
  Fastest:      0.1353 secs
  Average:      0.3005 secs
  Requests/sec: 132.1624
  

Response time histogram:
  0.135 [1]     |■
  0.187 [61]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.239 [21]    |■■■■■■■■■■■■■■
  0.290 [40]    |■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.342 [23]    |■■■■■■■■■■■■■■■
  0.394 [11]    |■■■■■■■
  0.445 [7]     |■■■■■
  0.497 [7]     |■■■■■
  0.548 [1]     |■
  0.600 [5]     |■■■
  0.652 [23]    |■■■■■■■■■■■■■■■


Latency distribution:
  10% in 0.1462 secs
  25% in 0.1637 secs
  50% in 0.2594 secs
  75% in 0.3852 secs
  90% in 0.6205 secs
  95% in 0.6376 secs
  99% in 0.6415 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0134 secs, 0.1353 secs, 0.6518 secs
  DNS-lookup:   0.0003 secs, 0.0000 secs, 0.0043 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0006 secs
  resp wait:    0.1255 secs, 0.0650 secs, 0.3958 secs
  resp read:    0.0031 secs, 0.0002 secs, 0.0213 secs

Status code distribution:
  [200] 200 responses

所以,我在项目目录下运行了source $HOME/.profile命令后,它就可以正常工作了,尽管我之前在另一个终端实例中已经运行过这个命令,我以为它是全局的。

问题是什么?

英文:

I downloaded and install the tar.gz file as noted here in the documentation: https://golang.org/doc/install

Edited my .profile to include these lines

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

The first line is from the documentation and the second and third lines are in the book Learn Go by O'Reilly.

My book wanted me to install the hey library for load testing websites. This was right after the section on installing Go.

(base) n@u-IdeaPad-3-15IIL05: hey https://www.golang.org

Command &#39;hey&#39; not found, but can be installed with:

sudo snap install hey       # version 0.1.2, or
sudo snap install hey-mail  # version 1.2.0
sudo apt  install hey       # version 0.1.2-2

See &#39;snap info &lt;snapname&gt;&#39; for additional versions.

(base) n@u-IdeaPad-3-15IIL05:/media/n/NTFSUbuWin/Projects/Golang$ source $HOME/.profile(base) n@u-IdeaPad-3-15IIL05:/media/n/NTFSUbuWin/Projects/Golang$ hey https://www.golang.org

Summary:
  Total:        1.5133 secs
  Slowest:      0.6518 secs
  Fastest:      0.1353 secs
  Average:      0.3005 secs
  Requests/sec: 132.1624
  

Response time histogram:
  0.135 [1]     |■
  0.187 [61]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.239 [21]    |■■■■■■■■■■■■■■
  0.290 [40]    |■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.342 [23]    |■■■■■■■■■■■■■■■
  0.394 [11]    |■■■■■■■
  0.445 [7]     |■■■■■
  0.497 [7]     |■■■■■
  0.548 [1]     |■
  0.600 [5]     |■■■
  0.652 [23]    |■■■■■■■■■■■■■■■


Latency distribution:
  10% in 0.1462 secs
  25% in 0.1637 secs
  50% in 0.2594 secs
  75% in 0.3852 secs
  90% in 0.6205 secs
  95% in 0.6376 secs
  99% in 0.6415 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0134 secs, 0.1353 secs, 0.6518 secs
  DNS-lookup:   0.0003 secs, 0.0000 secs, 0.0043 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0006 secs
  resp wait:    0.1255 secs, 0.0650 secs, 0.3958 secs
  resp read:    0.0031 secs, 0.0002 secs, 0.0213 secs

Status code distribution:
  [200] 200 responses

So it worked after I did source $HOME/.profile from the project directory although I already ran this command in a different terminal instance I thought globally.

What is the problem?

答案1

得分: 3

这归结于~/.profile在登录时被调用,而~/.bashrc在每次打开新终端时被调用。因此,如果你将go环境变量添加到~/.profile中,这些变量将在你注销然后重新登录后才被调用。

Golang文档建议使用~/.profile,因为这是推荐的环境变量位置。

还请注意,~/.bashrc和~/.profile都不是系统范围的。其他用户无法访问它们。系统范围的环境变量可以在/etc/profile中设置。

请参考这篇关于.bashrc和.profile之间差异的很好的解释,了解更多细节。

英文:

It comes down to the fact ~/.profile is sourced at login, while ~/.bashrc is sourced whenever you open a new terminal. So if you add the go environment variables to ~/.profile, the variables will not be sourced until you logout then login again.

The golang documentation recommends to use ~/.profile because it is the recommended place for environment variables.

Also please note that neither ~/.bashrc nor ~/.profile are system-wide. Other users don't have access them. System-wide environment variables could be set in /etc/profile.

Please refer to this great explanation about differences between .bashrc and .profile for more details.

答案2

得分: 2

与我阅读的关于将导出写入.profile以使其成为系统范围的文档相反,有一个评论直到作者删除它为止,该评论说将其写入我正在使用的任何shell中。
因此,在Ubuntu 20.04上,我将我的导出写入.bashrc文件中。

英文:

Contrary to whatever I read on documentation about writing exports to .profile to make them system-wide, there was a comment until the author deleted it saying write it to whichever shell I am using.
So on Ubuntu 20.04, I am writing my exports to .bashrc instead.

huangapple
  • 本文由 发表于 2021年7月12日 07:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68340611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定