Answers:
bash没有内置任何内容。您可以通过告诉它可能在.bashrc
每次显示提示时重新加载PROMPT_COMMAND
。
## Create a timestamp file, dated like the .bashrc that was read.
## There is a small race condition: if .bashrc is modified as the shell is
## just starting, before getting to this line, this instance won't detect
## that modification.
bashrc_timestamp_file=~/.bashrc-timestamp-$$
touch -r ~/.bashrc "$bashrc_timestamp_file"
## Remove the timestamp file on exit. The timestamp file will be left
## behind on a crash.
trap 'rm "$bashrc_timestamp_file"' EXIT HUP TERM INT QUIT
maybe_reload_bashrc () {
if [[ ~/.bashrc -nt $bashrc_timestamp_file ]]; then
. ~/.bashrc
fi
}
if [[ $PROMPT_COMMAND != *maybe_reload_bashrc* ]]; then
PROMPT_COMMAND="maybe_reload_bashrc
$PROMPT_COMMAND"
fi
额外的文件需要花费很多的麻烦。同样,它对您施加了约束.bashrc
:该文件必须是幂等的,即,您必须能够多次加载该文件,而不会产生不良影响。例如,在上面的代码段中,我小心maybe_reload_bashrc
地PROMPT_COMMAND
仅在不存在时才添加。
touch -r ~/.bashrc $bashrc_timestamp_file
在采购之后运行~/.bashrc
。我实际上差不多写了,但这不是必需的:它只是由源代码完成的.bashrc
。每次.bashrc
加载时都会更新时间戳文件,而不仅仅是在初始加载时更新。