我该如何从命令行重新加载 .bash_profile 文件呢?尽管退出登录然后重新登录可以让 shell 识别到 .bash_profile 的更改,但我希望能够按需重新加载它。
只需键入source ~/.bash_profile
。
或者,如果你喜欢节省按键,你可以键入. ~/.bash_profile
。
. ~/.bash_profile
只需确保您在其中没有任何对当前状态的依赖。
source
命令的别名。 - Graham P Heath.
比source
更早。 - Carl Norumsource
是 Bash 特有的实现方式,用于执行文件中的命令。 - Thirupathi Thangavelsource
在Bash中是点号'.
'的同义词,但在POSIX sh中不是,因此为了最大兼容性,请使用点号。” - Carl Norum只需输入:
. ~/.bash_profile
然而,如果你想让它在终端启动时自动运行,而不是每次打开终端都运行它,你可以把. ~/.bash_profile
添加到~/.bashrc
文件中。
注意:
当你打开一个终端时,终端会以(非登录)交互模式启动bash,这意味着它会source ~/.bashrc
。
~/.bash_profile
仅在bash以交互登录模式启动时被source。通常情况下,只有在你通过控制台登录(Ctrl+Alt+F1...F6),或者通过ssh
连接时才会出现。
bash -l
。这将分叉您的Shell并打开Bash的另一个子进程。-l
参数告诉Bash以登录Shell运行。这是必须的,因为.bash_profile不会在非登录Shell上运行。欲了解更多相关信息,请点击此处。exec bash -l
。以上命令将不会分叉当前Shell,而是完全替换它,所以当您输入exit
时,它将完全终止,而不是返回到以前的Shell。你也可以使用这个命令重新加载该用户的~/.bash_profile文件。确保使用破折号。
su - username
.
"是"source
"的别名。在man页面上,它靠近“在当前shell环境中执行并返回最后一个执行的命令的退出状态”(虽然不是很有帮助(太简洁了))。 - Peter Mortensencd
进入用户的主目录. .bash_profile
重新加载配置文件cd
命令即可回到主目录,不需要输入 ~
。 - roNn23cd
命令 - 您可以直接从当前目录重新加载它:. ~/.bash_profile
。 - Alex Villa如果 .bash_profile 文件不存在,你可以尝试运行以下命令:
. ~/.bashrc
或者
source ~/.bashrc
使用.bashrc而不是.bash_profile。
关于bashrc,您可以找到更多信息。
使用
alias reload!=". ~/.bash_profile"
如果想通过函数添加日志:
function reload! () {
echo "Reloading bash profile...!"
source ~/.bash_profile
echo "Reloaded!!!"
}
. ~/.bash_profile
或者source ~/.bash_profile
。 - 7urkm3n
". ~/.bash_profile"
,您可以将其设置为别名"BASHRELOAD",例如alias BASHRELOAD=". ~/.bash_profile"
,这样做的话,您只需要键入"br"即可运行该命令。 - boboboboalias reload='source ~/.bash_profile && echo "文件 .bash_profile 已成功重新加载" || echo "语法错误,无法导入文件"';
- Cristian Batista