[英]Trying to change the color of bash prompt depending on CWD. Can't get it to update after changing directories
我有两个存储驱动器,正在尝试配置我的 bashrc 文件来执行此操作:
如果 cwd 在驱动器 B 上:
到目前为止,我已经能够使用以下代码完成更改颜色,并且可以通过使用 '\\W' 修饰符截断整个提示以仅显示基本名称 - 但我正在努力解决如何通过指定底部目录来修剪 PROMPT_COMMAND ...
IE
/media/devj/2a24a03f-99a1-44bd-9a53-341zdd68334b/home/dev --> /home/dev/
解决方案:感谢您的帮助,这就是我最终要做的:
#Check if we are accessing my home folder from secondary drive
#If so change the color and trim the filepath to secondary /home
bash_prompt_command() {
root_path=`pwd | awk -F/ '{print $(NF-(NF-2))}'`
if [ $root_path = media ]; then #Shared drive
#Trim path up to the home directory
trim_path=${PWD#/media/devj/9a84a09e-80e9-44bd-9a53-342e3d48334c/}
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$trim_path\[\033[38;5;214m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[38;5;214m\]$(parse_git_branch)\[\033[00m\]\$ '
fi
}
# init it by setting PROMPT_COMMAND
PROMPT_COMMAND=bash_prompt_command
trim_path=${PWD#/*/*/*/}
替换要从路径开头省略的许多目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.