首页 > 解决方案 > Ubuntu:安装新软件后添加到 PATH 的正确方法,.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 的 Learn Go 一书。

我的书要我安装hey用于负载测试网站的库。这是在安装 Go 的部分之后。

(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尽管我已经在我认为全局的不同终端实例中运行了此命令,但在我从项目目录中执行之后它仍然有效。

问题是什么?

标签: go

解决方案


归结为 ~/.profile 是在登录时获取的,而 ~/.bashrc 是在您打开新终端时获取的。因此,如果您将 go 环境变量添加到 ~/.profile,则在您注销然后再次登录之前不会获取变量。

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

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

有关更多详细信息,请参阅有关.bashrc 和 .profile 之间差异的精彩说明。


推荐阅读