Try   HackMD
tags: tutorials linux ubuntu

修改使用者設定檔 ~/.bashrc

最好在修改之前先備份

$ cp ~/.bashrc ~/.bashrc_bk

修改 ~/.bashrc

Set prompt style

  • 約 46 行: 將 force_color_prompt=yes 取消註解以開啟 bash 顯示顏色

  • 約 60 行: 修改 PS1 改顏色 (修改 \[\XXX[XX;XX\])

    • 原始 PS1
      ​​​​​​​​PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
      • 效果為 {user_name}@{host_name}:{work_path}$ 
    • 修改為
      ​​​​​​​​PS1='${debian_chroot:+($debian_chroot)}\[\033[1;36m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
      • 效果為 {user_name}@{host_name}:{work_path}$ 
    • \u: user name,使用者名稱
    • \h: host name,電腦名稱
    • \w: work path,當前工作路徑

    簡單來說就是

    1. \u 之前的 \[\033[01;32m\] 修改為 \[\033[1;36m\]
    2. @ 之前加入 \[\033[01;33m\]
    3. \h 之前加入 \[\033[01;31m\]
linux color code
color code light color code
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
lazy mode
$ cp ~/.bashrc ~/.bashrc_bk && sed -i '0,/PS1.*/ s/PS1.*/'"\PS1=\'\$\{debian_chroot:\+\(\$debian_chroot\)\}\\\[\\\033\[01;36m\\\]\\\u\\\[\\\033\[00;33m\\\]@\\\[\\\033\[01;31m\\\]\\\h\\\[\\\033\[00m\\\]:\\\[\\\033\[01;34m\\\]\\\w\\\[\\\033\[00m\\\]$ \'"'/' ~/.bashrc && sed -i 's/^#force_color_prompt=yes/force_color_prompt=yes/' ~/.bashrc

Auto call ~/.bash_aliases when open new shell

  • 如果 ~/.bash_aliases 存在,之後進 bash 會由 ~/.bashrc 呼叫 ~/.bash_aliases 並自動設定
    • 編輯 ~/.bash_rc
    ​​​​$ vim ~/.bash_rc
    • 內容填入
    ​​​​if [ -f ~/.bash_aliases ]; then ​​​​ source ~/.bash_aliases ​​​​fi
    • 預設 ~/.bash_aliases 只會在第一次登入的 bash 呼叫一次,之後再開的 bash 不會套用
    • ~/.bash_profile 同上,若要所有 shell 都套用設置只需把上述內容的 ~/.bash_aliases 都修改為~/.bash_profile 即可
bash flow






G



empty




empty2





bp_ex

~/.bash_profile
exists



empty->bp_ex


N



empty3




empty4




prompt

bash prompt



empty4->prompt





empty5




login

login shell



new_shell

open new shell




ep_ex

/etc/profile
exists



login->ep_ex





br_ex

~/.bashrc
exists



new_shell->br_ex





prompt->empty3


N



ep_ex->empty




sp

source /etc/profile



ep_ex->sp


Y



rp_sh

run all .sh
under /etc/profile.d folder



sp->rp_sh





rp_sh->bp_ex





bp_ex->empty3




sbp

source ~/.bash_profile



bp_ex->sbp


Y




sbp->br_ex





br_ex->empty4

N



sbr

source ~/.bashrc



br_ex->sbr


Y



ebr_ex

/etc/bashrc
exists



sbr->ebr_ex





ebr_ex->empty4

N



sebr

source /etc/bashrc



ebr_ex->sebr


Y



sebr->prompt





lazy mode
$ echo -e "if [ -f ~/.bash_aliases ]; then\n source ~/.bash_aliases\nfi" >> ~/.bash_rc

修改 ~/.bash_aliases

  • alias 加在 ~/.bash_aliases 內,定義自訂快捷指令,之後進 bash 會由 ~/.bashrc 呼叫 ~/.bash_aliases 並自動設定
    • 編輯 ~/.bash_aliases (若不存在,vim 指令會自動建立該檔案)
    ​​​​$ vim ~/.bash_aliases
    • 內容填入
    ​​​​alias nv='nvidia-smi' ​​​​alias wnv='watch -n 1 nvidia-smi' ​​​​alias wwnv='watch -n 0.1 nvidia-smi'

    定義快捷指令,可以以更短、更便捷的指令來呼叫本來想執行的指令

    • 這裡是將 nv 設定為 nvidia-smi 指令,意即只要輸入 nv 即為 nvidia-smi 指令
lazy mode
$ echo -e "alias nv='nvidia-smi'\nalias wnv='watch -n 1 nvidia-smi'\nalias wwnv='watch -n 0.1 nvidia-smi'" >> ~/.bash_aliases

修改 ~/.bash_profile

  • profile 加在 ~/.bash_profile 內,定義環境變數,之後進 bash 會由 ~/.bashrc 呼叫 ~/.bash_profile 並自動設定
    • 編輯 ~/.bash_profile (若不存在,vim 指令會自動建立該檔案)
    ​​​​$ vim ~/.bash_profile
    • 內容填入
    ​​​​# clean up history: ​​​​export HISTCONTROL=erasedups:ignoreboth
    • 避免儲存之前執行的重複指令(連續重複指令按 可以只顯示一次)
    • 預設 ~/.bash_profile 只會在第一次登入的 bash 呼叫一次,之後再開的 bash 不會套用,若要在所有 bash 都套用,要把這個設定加在 ~/.bashrc 或者照 這個 修改
lazy mode
$ echo -e "# clean up history:\nexport HISTCONTROL=erasedups:ignoreboth" >> ~/.bash_profile

更改完畢後立即套用設置 (Optional)

source ~/.bashrc

即使不立即套用,之後重開 shell 也會自動套用