How can I shortern my command line prompt's current directory?

前端 未结 10 1944
野趣味
野趣味 2020-11-29 17:08

I am using Ubuntu and I am tired of this long prompts in bash when I am working with some deep directory hierarchy. So, I would like to tweak my PS1 to shorten the working d

10条回答
  •  猫巷女王i
    2020-11-29 17:37

    generatePwd(){
      set -- "`pwd | sed -e s.${HOME}.~.g`"
      IFS="/"; declare -a array=($*)
      srt=""
      count=0
      for i in ${!array[@]}; do
          # echo ${array[i]} - $i/${#array[@]}
          if [[ $i == 0 ]]; then
            srt+=""
          elif [[ $i == $((${#array[@]}-1)) ]] || [[ $i == $((${#array[@]}-2)) ]]; then
              srt+="/${array[i]}"
          else
            count=$((${count}+1))
          fi
      done
      if [[ $count != 0 ]]; then
        srt="${array[0]}/.$count.$srt"
      else
        srt="${array[0]}$srt"
      fi
      echo "${srt}"
    }
    

    export PS1

    PS1="\$(generatePwd)"
    

    Console

    $ ~/.3./machine-learning/deep-learning-computer-vision
    

提交回复
热议问题