Customize Your Bash Prompt (PS1)

Yes Lee
3 min readSep 18, 2019

--

前幾天我 Terminal 上的 git-completion 壞了,之前都是無腦照別人的 Tutorial 裝,這次在修的同時多研究了一下 Script,才發現原來 Bash Prompt 很容易地就能調整成自己想要的樣子!

TL;DR

Check HalloweenBash and update the environment variablePS1 to customize your bash prompt!

Update $PS1

PS1 就是用來控制 Bash Prompt 的環境變數,改一下他的值立刻就能改變 Prompt,例如:在 Terminal 裡 export PS1="\W\\$" ,結果會如下圖。

Update PS1 to update bash prompt

至於 PS1 的值該怎麼設定則可以參考 HalloweenBash 這個網站。

HalloweenBash, a bash profile generator website.

Setup PS1 in ~/.bash_profile

最後,在 ~/.bash_profileexport PS1 才能讓所有 Terminal 都吃到一樣的設定,下面是我自己結合了 git-prompt 的例子。

# ~/.bash_profile# base prompt
PS1="\[\e[36;40m\]\W\\$ \[\e[0m\]"
.
.
# git prompt
if [ -f ~/.git-prompt.bash ]; then
source ~/.git-prompt.bash
PS1='\[\e[32;40m\]$(__git_ps1 "%s ")\[\e[0m\]'$PS1
fi
.
.
# setup prompt
export PS1
My customized bash prompt 😍

--

--

Yes Lee
Yes Lee

Written by Yes Lee

Frontend Engineer in Taiwan.

No responses yet