###### tags: `tutorials` `linux` `ubuntu` # 修改使用者設定檔 `~/.bashrc` :::warning 最好在修改之前先備份 ```bash= ! $ cp ~/.bashrc ~/.bashrc_bk ``` ::: ## 修改 `~/.bashrc` ### Set prompt style - 約 46 行: 將 `force_color_prompt=yes` 取消註解以開啟 bash 顯示顏色 - 約 60 行: 修改 `PS1` 改顏色 (修改 `\[\XXX[XX;XX\]`) - 原始 `PS1` ~~~bash= ! PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' ~~~ - 效果為 <span style="background:black"><font color="#4e9a06">{user_name}@{host_name}</font><font color="white">:</font><font color="#32afff">{work_path}</font><font color="white">$</font> </span> - 修改為 ~~~bash= ! PS1='${debian_chroot:+($debian_chroot)}\[\033[1;36m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' ~~~ - 效果為 <span style="background:black"><font color="#06989a">{user_name}</font><font color="c4a000">@</font><font color="#ef2929">{host_name}</font><font color="white">:</font><font color="#32afff">{work_path}</font><font color="white">$</font> </span> - `\u`: user name,使用者名稱 - `\h`: host name,電腦名稱 - `\w`: work path,當前工作路徑 :::info 簡單來說就是 1. 將 `\u` 之前的 `\[\033[01;32m\]` 修改為 `\[\033[1;36m\]` 2. 在 `@` 之前加入 `\[\033[01;33m\]` 3. 在 `\h` 之前加入 `\[\033[01;31m\]` ::: :::spoiler 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| ::: :::spoiler lazy mode ```bash= ! $ 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` ```bash= ! $ vim ~/.bash_rc ``` - 內容填入 ~~~bash= ! if [ -f ~/.bash_aliases ]; then source ~/.bash_aliases fi ~~~ :::danger - 預設 `~/.bash_aliases` 只會在第一次登入的 bash 呼叫一次,之後再開的 bash 不會套用 - `~/.bash_profile` 同上,若要所有 shell 都套用設置只需把上述內容的 `~/.bash_aliases` 都修改為`~/.bash_profile` 即可 ::: :::spoiler bash flow ```graphviz digraph G { node []; edge []; splines="ortho"; rankdir="LR" empty [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] empty2 [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] empty3 [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] empty4 [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] empty5 [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] empty5 [ label = ""; shape=box; width=0; height=0; color="#FFFFFFFF" ] login [ label = "login shell"; shape = oval; ]; new_shell [ label = "open new shell"; shape = oval; ]; prompt [ label = "bash prompt"; shape = oval; ]; ep_ex [ label = "/etc/profile\nexists"; shape = diamond; ]; sp [ label = "source /etc/profile"; shape = rect; ]; rp_sh [ label = "run all .sh\nunder /etc/profile.d folder"; shape = rect; ]; bp_ex [ label = "~/.bash_profile\nexists"; shape = diamond; ]; sbp [ label = "source ~/.bash_profile"; shape = rect; ]; br_ex [ label = "~/.bashrc\nexists"; shape = diamond; ]; sbr [ label = "source ~/.bashrc"; shape = rect; ]; ebr_ex [ label = "/etc/bashrc\nexists"; shape = diamond; ]; sebr [ label = "source /etc/bashrc"; shape = rect; ]; empty -> empty2 [style=invis]; login -> ep_ex; new_shell -> br_ex; ep_ex -> sp [ xlabel = "Y"; color=Green ]; sp -> rp_sh; rp_sh -> bp_ex; bp_ex -> sbp [ label = "Y"; color=Green ]; sbp -> prompt [style=invis]; bp_ex -> empty3 [ arrowhead=none; color=Red; ]; prompt -> empty3 [ label = "N"; color=Red; dir=back ]; sbp -> br_ex; br_ex -> sbr [ label = "Y"; color=Green ]; br_ex -> empty4 [ label = "N"; arrowhead=none; color=Red; ]; empty4 -> prompt [ color=Red; ]; sbr -> ebr_ex; ebr_ex -> sebr [ label = "Y"; color=Green ]; ebr_ex -> empty4 [ label = "N"; arrowhead=none; color=Red; ]; sebr -> prompt; ep_ex -> empty [arrowhead=none; color=Red;]; empty -> bp_ex [ label = "N"; color=Red; ]; login -> new_shell [style=invis]; { rank=same; login; ep_ex; sp; rp_sh; bp_ex; empty2; empty3; } { rank=same; sbp; } { rank=same; br_ex; sbr; prompt; ebr_ex; sebr; new_shell; } { rank=min; empty } { rank=max; empty4; } } ``` ::: :::spoiler lazy mode ```bash= ! $ 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` 指令會自動建立該檔案) ```bash= ! $ vim ~/.bash_aliases ``` - 內容填入 ~~~bash= ! alias nv='nvidia-smi' alias wnv='watch -n 1 nvidia-smi' alias wwnv='watch -n 0.1 nvidia-smi' ~~~ :::info 定義快捷指令,可以以更短、更便捷的指令來呼叫本來想執行的指令 - 這裡是將 `nv` 設定為 `nvidia-smi` 指令,意即只要輸入 `nv` 即為 `nvidia-smi` 指令 ::: :::spoiler lazy mode ```bash= ! $ 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` 指令會自動建立該檔案) ```bash= ! $ vim ~/.bash_profile ``` - 內容填入 ~~~bash= ! # clean up history: export HISTCONTROL=erasedups:ignoreboth ~~~ :::info - 避免儲存之前執行的重複指令(連續重複指令按 `↑` 可以只顯示一次) ::: :::danger - 預設 `~/.bash_profile` 只會在第一次登入的 bash 呼叫一次,之後再開的 bash 不會套用,若要在所有 bash 都套用,要把這個設定加在 `~/.bashrc` 或者照 [這個](#Auto-call-bash_aliases-when-open-new-shell) 修改 ::: :::spoiler lazy mode ```bash= ! $ echo -e "# clean up history:\nexport HISTCONTROL=erasedups:ignoreboth" >> ~/.bash_profile ``` ::: ## 更改完畢後立即套用設置 (Optional) ~~~bash= source ~/.bashrc ~~~ :::info 即使不立即套用,之後重開 shell 也會自動套用 :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up