T A*_*nna 2 linux bash ubuntu .profile apache-kafka
我是 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"
Run Code Online (Sandbox Code Playgroud)
之后,我在 $PATH 上做了一个 echo,我可以看到添加到 PATH 中的 kafka 路径。发布其中,我输入 kafka,然后输入 tab,然后我可以看到与 kafka 相关的可选命令。
然后教程告诉编辑 .bashrc 文件。它已经在那里了。这是一个相当大的文件。我在文件末尾添加了以下行(根据教程):
. ~/.profile
Run Code Online (Sandbox Code Playgroud)
之后,我按照教程打开了另一个终端,看到在 kafka 和 tab 之后我仍然获得了所有与 kafka 相关的选项。我在终端上看到了,我没有得到我的用户名,这是一个空白的终端窗口。然后我编辑了 .bashrc 以删除我添加的行,然后尝试打开一个新终端,我可以在终端上看到我的用户名。然后我关闭了所有终端。打开一个新的并输入kafka和tab,我没有像之前那样得到任何选项。然后我打开了 .profile 文件,我可以看到仍然添加了 kafka 路径。然后,我尝试 echo $PATH,这一次,kafka 路径不在那里。
我真的很困惑这里发生了什么。你能解释一下吗,让我知道每次打开终端时如何加载 .profile,为什么我在 PATH 上做回声时看不到 kafka 路径。
发生了什么:
采购:
当您运行该命令时,. somefile您正在将该文件导入当前 shell,这基本上意味着它会运行当前 shell 中的每个命令somefile。
某些文件是在特定条件下自动获取的。
外壳类型:
交互式外壳:用于键入命令和接收输出的外壳。在 bash 中,您可以通过以下方式创建交互式 shell:
登录外壳:首次登录时创建的外壳。IE。当您第一次通过 ssh 进入服务器或登录到没有 GUI 的机器时收到的 shell。
非登录交互式外壳:不是登录外壳而是交互式的外壳。IE。打开桌面终端应用程序时创建的外壳或bash通过 ssh 登录后运行时获得的外壳
~/.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
参考:
笔记:
不要担心它不会出现在登录 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
Run Code Online (Sandbox Code Playgroud)
感谢 cdarke 对术语的更正和关于登录配置如何工作的注意事项