简体   繁体   English

更改 .bashrc 和 .bash_profile 后终端无法正确更新

[英]terminal not updating properly after changing .bashrc and .bash_profile

I'm just trying to update my.bashrc and.bash_profile file on my new mac.我只是想在我的新 Mac 上更新 my.bashrc 和 .bash_profile 文件。 My files look like so:我的文件如下所示:

//.bash_rc //.bash_rc

orange=$(tput setaf 166);
yellow=$(tput setaf 228);
green=$(tput setaf 71);
white=$(tput setaf 15);
bold=$(tput bold);
reset=$(tput sgr0);

PS1="\[${bold}\]\n";
PS1+="\[${orange}\]\u";  # username
PS1+="\[${white}\] at ";
PS1+="\[${yellow}\]\h";  # host
PS1+="\[${white}\] in ";
PS1+="\[${green}\]\W :";   # working directory
PS1+="\n";
PS1+="\[${white}\]\$ \[${reset}\]"; # '$' and reset color
export PS1;

//.bash_profile //.bash_profile

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

My terminal looks like this:我的终端看起来像这样: 在此处输入图像描述

I can't figure out what I've entered wrong.我无法弄清楚我输入了什么错误。 Any help appreciated.任何帮助表示赞赏。 Thanks谢谢

First, you are using zsh , not bash ;首先,您使用的是zsh ,而不是bash prompts are configured entirely differently.提示的配置完全不同。

Second, you need not export PS1 , as any process that cares about its value will be sourcing .zshrc .其次,您不需要导出PS1 ,因为任何关心其价值的进程都将采购.zshrc

The following should work, placed in .zshrc :以下应该可以工作,放置在.zshrc中:

# The variables aren't necessary, but retained for
# ease of updating. You could specify %F{15} directly
# for %F{$white}, e.g.
orange=166
yellow=228
green=71
white=15

PS1=$'%B\n'
PS1+="%F{$orange}%n"
PS1+="%F{$white} at "
PS1+="%F{$yellow}%m";
PS1+="%F{$white} in "
PS1+="%F{$green}%~ :"
PS1+=$'\n'
PS1+="%F{$white}%# %f%b"

%B and %b turn bold on and off, respectively. %B%b分别打开和关闭粗体。 %F{...} changes the current foreground color; %F{...}改变当前的前景色; %f resets to the terminal default. %f重置为终端默认值。 The \ escapes from bash are replaced with % escapes. bash中的\转义符被替换为%转义符。 You don't need the zsh equivalent of \[...\] anywhere, because you aren't using any raw byte sequences to control formatting.您在任何地方都不需要与\[...\]等效的zsh ,因为您没有使用任何原始字节序列来控制格式。

The entire thing could be reduced to a single line:整个事情可以简化为一行:

PS1=$'%B\n%F{166}%n%F{15} at %F{228}%m%F{15} in %F{71}%~ :\n%F{15}%# %f%b'

though the longer version is more maintainable.虽然较长的版本更易于维护。

Finally, note that (unlike bash ) zsh sources .zshrc for all interactive shells.最后,请注意(与bash不同) zsh为所有交互式 shell 提供.zshrc源。 If it's also a login shell, it sources .zprofile , before, not instead of, .zshrc .如果它也是一个登录 shell,它会在.zprofile之前而不是代替.zshrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM