简体   繁体   English

如何在bash中检查当前路径?

[英]How can I check for the current path in bash?

I'm working 我在工作

on multiple projects : A, B, C, D, E and F. I go though all my projects via Terminal , and swapping between them via Terminal Tabs . 在多个项目中:A,B,C,D,E和F.我通过终端进行所有项目,并通过终端标签交换它们。

Sometimes, I confuse between projects because they have the same text color, in this case is yellow. 有时,我在项目之间混淆,因为它们具有相同的文本颜色,在这种情况下是黄色。

在此输入图像描述


I'm trying 我正在努力

to achieve something like this: 实现这样的目标:

在此输入图像描述

I would like to assign a different color base on the current path of the project. 我想在项目的当前路径上分配不同的颜色基础。

How to do I check for current path in bash ? 如何检查bash中的当前路径?


.bash_profile .bash_profile中

#================================
#            Colors             =
#================================

black="\[\033[0;30m\]"
blue="\[\033[1;37m\]"
green="\[\033[0;32m\]"
cyan="\[\033[0;36m\]"
red="\[\033[0;31m\]"
purple="\[\033[0;35m\]"
brown="\[\033[0;33m\]"
lightgray="\[\033[0;37m\]"
darkgray="\[\033[1;30m\]"
lightblue="\[\033[1;34m\]"
lightgreen="\[\033[1;32m\]"
lightcyan="\[\033[1;36m\]"
lightred="\[\033[1;31m\]"
lightpurple="\[\033[1;35m\]"
yellow="\[\033[1;33m\]"
white="\[\033[1;37m\]"
nc="\[\033[0m\]"

if [ "\w" == "~/dev/projects/biv2" ]; then
  export PS1="──$white[$blue\w$white] \n└── $white"
fi

// Default Color
export PS1="──$white[$yellow\w$white] \n└── $white"

You'll have to use PROMPT_COMMAND to check what the current directory is just before displaying the prompt, and set the value of PS1 accordingly. 在显示提示之前,您必须使用PROMPT_COMMAND来检查当前目录是什么,并相应地设置PS1的值。

prompt_cmd () {
    case $PWD in
        ~/dev/projects/biv2) dircolor=$yellow ;;
        ~/dev/projects/other) dircolor=$blue ;;
        # and so on. For any other directory,
        *) dircolor=$green
    esac
    PS1="──$white[$dircolor\w$white] \n└── $white"
}

PROMPT_COMMAND=prompt_cmd

Without PROMPT_COMMAND , you could do something like 没有PROMPT_COMMAND ,你可以做类似的事情

set_dir_color () {
    case $PWD in
        ~/dev/projects/biv2) dircolor=$yellow ;;
        ~/dev/projects/other) dircolor=$blue ;;
        # and so on. For any other directory,
        *) dircolor=$green
    esac
    echo "$dircolor"
}

PS1="──$white[\$(set_dir_color)\w$white] \n└── $white"

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

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