[Share Experiences] 修改.bashrc的PS1,区分当前子系统
Tofloor
poster avatar
MIDAC
deepin
2025-02-17 15:22
Author

Distrobox 子系统功能很好用,但有一个问题比较糟心,

所有的子系统都用一样的终端,经常不知道自己在哪里做事。

于是有以下简易办法。

警告,修改前注意备份。

修改.bashrc,在PS1相关代码前加上:

#查找 /etc/os-release 并且读取name后的内容
#已知适用Arch、Ubuntu、Deepin,如果有任何发行版不适用,请回复我cat /etc/os-release的结果,如果没有,尝试cat /etc/*release

os_release_name=$(grep -ioP '(?<=^name=")[^" ]+' /etc/os-release)

#用${os_release_name}替换PS1中的\h

然后将所有PS1中的 \h(电脑名)替换为 ${os_release_name}(发行版名)。

保存后再次打开终端,结果:

图片.png

替换后的PS1部分(供参考,自己改,不要复制我这个):

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@${os_release_name}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@${os_release_name}:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@${os_release_name}: \w\a\]$PS1"
    ;;
*)
    ;;
esac
Reply Favorite View the author
All Replies
wlly-lzh
deepin
2025-02-17 15:36
#1

like

Reply View the author
zccrs
deepin
2025-02-17 16:00
#2

666, 这个问题官方版本肯定要默认解决的,不然都分不清谁是谁

Reply View the author