gpt4 book ai didi

linux - 遇到 Ubuntu .profile 和 .bashrc 问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:09 26 4
gpt4 key购买 nike

我是 Linux 新手。我目前正在在线阅读 Kafka 的设置教程。它说要将我的 kafka bin 目录的路径添加到我的 .profile 文件中,我的操作如下:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

# set PATH so it includes user's private bin directories
DOCKER="/usr/local/bin/docker-compose"
PATH="$HOME/bin:$HOME/.local/bin:$PATH:$DOCKER"
# Ubuntu make installation of Ubuntu Make binary symlink
PATH=/home/username/.local/share/umake/bin:$PATH
cat ~/.ssh/config.d/* > ~/.ssh/config
PATH="$PATH:/home/username/softwares/kafka/kafka_2.11-1.0.0/bin"

在那之后,我在 $PATH 上做了一个 echo,我可以看到 kafka 路径被添加到 PATH 中。发布后,我输入了 kafka,然后输入 tab,然后我可以看到与 kafka 相关的可选命令。

然后教程告诉我们编辑 .bashrc 文件。它已经在那里了。这是一个很大的文件。我在文件末尾添加了以下行(根据教程):

. ~/.profile

在那之后,我按照教程打开了另一个终端,看到在 kafka 和 tab 之后我仍然得到所有与 kafka 相关的选项。我在终端上看到,我没有得到我的用户名,它是一个空白的终端窗口。然后我编辑 .bashrc 以删除我添加的行,然后尝试打开一个新终端,我可以在终端上看到我的用户名。然后我关闭了所有终端。打开一个新的并输入 kafka 和 tab,但我没有像之前那样得到任何选项。然后我打开 .profile 文件,我可以看到仍然添加了 kafka 路径。然后,我尝试回显 $PATH,这一次,kafka 路径不存在。

我真的很困惑这里发生了什么。您能否稍微解释一下,让我知道每次打开终端时如何加载 .profile,以及为什么当我在 PATH 上执行回显时,我再也看不到 kafka 路径了。

最佳答案

发生了什么:

采购:

当您运行命令 时。 somefile 您将该文件采购到您当前的 shell 中,这基本上意味着它运行当前 shell 中 somefile 中的每个命令。

某些文件在特定条件下会自动获取。

外壳类型:

Interactive Shell:用于输入命令和接收输出的 shell。在 bash 中,您可以通过以下方式创建交互式 shell:

  1. 使用 bash shell 登录
  2. 从终端运行 bash

登录外壳:首次登录时创建的外壳。 IE。当您第一次通过 ssh 连接到服务器或登录到一台没有 GUI 的机器时收到的 shell。

非登录交互式 Shell:不是登录 shell 但具有交互性的 Shell。 IE。打开桌面终端应用程序时创建的 Shell 或通过 ssh 登录后运行 bash 时获得的 Shell

~/.profile 和 ~/.bash_profile 以及 ~/.bash_login 可以由登录 shell 自动获取 所以,当你第一次登录或手动获取 (.).profile 时,你可能会得到这个路径

请注意,如果 .bash_profile 存在并且可读,那么 Bash 将不会读取 .bash_login 或 .profile。 https://askubuntu.com/questions/98433/run-a-script-on-login-using-bash-login

登录 shell 查找 ... "~/.bash_profile、~/.bash_login 和 ~/.profile,按此顺序,并从第一个存在且可读的命令开始读取和执行命令" man bash(来源:cdarke)

之后您可能试图在桌面终端或辅助 bash shell 中打开 bash,但您没有获得新路径,因为只有 ~/.bashrc —— 而不是 ~/.profile 来源于非登录交互式 Shell,并且您的 ~/.bashrc 中没有此 PATH 指令

解决方案:

PATH="$PATH:/home/username/softwares/kafka/kafka_2.11-1.0.0/bin" 添加到您的 ~/.bashrc~/.profile

引用:

https://serverfault.com/questions/261802/what-are-the-functional-differences-between-profile-bash-profile-and-bashrc

注意:

不要担心它不会出现在登录 shell 中,因为如您所见,.profile 调用 .bashrc

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

感谢 cdarke 对术语的更正和登录配置如何工作的注意事项

关于linux - 遇到 Ubuntu .profile 和 .bashrc 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52796845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com