Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

縮短 Linux 終端機提示路徑:修改 PS1 只顯示當前目錄

編輯:Linux 系統常見問題解答

預設 Bash 提示符 PS1 顯示完整路徑(\w),深層目錄下會佔滿整行,影響閱讀。只需將 \w 改為 \W(僅顯示當前目錄名),即可大幅精簡提示列。

修改步驟

  1. 編輯 ~/.bashrc:vim ~/.bashrc
  2. 搜尋 PS1,找到類似區塊:
    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$ '
    fi
  3. 將 \w 全部改為 \W(大寫 W)。
  4. 套用變更:source ~/.bashrc

效果對比

# 修改前
jack@ubuntu:~/trunk-new/build_dir/target-mips_34kc_uClibc-0.9.33.2/linux-ar71xx_generic/linux-3.10.17/net/netfilter/commercials$

# 修改後
jack@ubuntu:commercials$

提示列瞬間清爽,專注於當前工作目錄。

Copyright © Windows教程網 All Rights Reserved