個人化你的Bash Prompt
最近在研究Bash Prompt的呈現方式與顏色設定
首先要先知道顯示資訊的方式:
- \u: current username
- \h: hostname up to the first ., \H: full hostname
- \w: current working directory, \W: same, but only the basename
- $(__git_ps1 “%s”): your current git branch if you’re in a git directory, otherwise nothing
- \$: if the effective UID is 0: #, otherwise $
- \d: the date in “Weekday Month Date” format (e.g., “Tue May 26”)
- \t: the current time in 24-hour HH:MM:SS format, \T: same, but 12-hour format, \@: same, but in 12-hour am/pm format
- \n: newline
- \r: carriage return
- \\: backslash
顏色:
我們要將顏色色碼放在\[\em\]中間,像\[\e[‹色碼›m\]
色碼選擇可參考這
開始設定:
username: unama
hostname: notebook
例如將下列這行加入~/.bashrc
PS1='\u@\h \w \$ '
並到~/執行
$ source .bashrc
就會顯示
unama@notebook ~ $
如果要加顏色則在要變色的資訊前加\[\e[‹色碼›m\]即可
ex.
PS1='[\033[01;32m\]\u@\h\[\033[01;33m\]\w\[\033[01;31m\]>\[\033[00m\]'
顯示Git Branch
PS1='\u@\h \w$(__git_ps1 "(%s)") \$ '
如果出現以下錯誤訊息
-bash: __git_ps1: command not found
可以試著執行
curl -o ~/.git-prompt.sh \ https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
source ~/.git-prompt.sh
2017.2.7
bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie