Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

10 วิธีปรับแต่ง PS1 ให้ Linux Bash Prompt อ่านง่ายและสวยงาม

Photo courtesy of f1r3storm85

ในบทความก่อนหน้า เราได้พูดถึง Linux Environment Variables PS[1-4] และ PROMPT_COMMAND หากใช้อย่างมีประสิทธิภาพ PS1 จะให้ข้อมูลมีค่าอยู่ที่ Command Prompt ทันที

ใน Tomb Raider, Angelina Jolie มี Gadgets และอาวุธครบครันที่นิ้วมือเพื่อแก้ปริศนาด้วยสไตล์ แม้ Gadget และ Style ของ Angelina Jolie จะยากที่จะเทียบเท่า แต่เรามาลองทำให้ Linux Prompt ที่ดีเก่าแก่นี้ Functional และ Stylish ด้วย 10 ตัวอย่างในบทความนี้

1. แสดง Username, Hostname และ Current Working Directory ใน Prompt

PS1 ในตัวอย่างนี้แสดงข้อมูล 3 อย่างใน Prompt:

  • \u – Username
  • \h – Hostname
  • \w – Full Path ของ Current Working Directory
-bash-3.2$ export PS1="\u@\h \w> "
ramesh@dev-db ~> cd /etc/mail
ramesh@dev-db /etc/mail>

2. แสดงเวลาปัจจุบันใน Prompt

ใน PS1 Environment Variable คุณสามารถรัน Linux Command ใดๆ ได้โดยระบุในรูปแบบ $(linux_command) ในตัวอย่างต่อไปนี้ คำสั่ง $(date) ถูกรันเพื่อแสดงเวลาปัจจุบันใน Prompt

ramesh@dev-db ~> export PS1="\u@\h [$(date +%k:%M:%S)]> "
ramesh@dev-db [11:09:56]>

คุณยังสามารถใช้ \t แสดงเวลาปัจจุบันในรูปแบบ hh:mm:ss ได้ดังนี้:

ramesh@dev-db ~> export PS1="\u@\h [\t]> "
ramesh@dev-db [12:42:55]>

หรือใช้ \@ แสดงเวลาในรูปแบบ 12-hour am/pm:

ramesh@dev-db ~> export PS1="[\@] \u@\h> "
[04:12 PM] ramesh@dev-db>

3. แสดง Output ของ Linux Command ใดๆ ใน Prompt

คุณสามารถแสดง Output ของ Linux Command ใดๆ ใน Prompt ตัวอย่างต่อไปนี้แสดง 3 รายการคั่นด้วย | (Pipe) ใน Command Prompt:

  • \!: History Number ของคำสั่ง
  • \h: Hostname
  • $kernel_version: Output ของคำสั่ง uname -r จาก Variable $kernel_version
  • \$?: Status ของคำสั่งล่าสุด
ramesh@dev-db ~> kernel_version=$(uname -r)
ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> "
473|dev-db|2.6.25-14.fc9.i686|0>

4. เปลี่ยนสี Foreground ของ Prompt

แสดง Prompt สีฟ้า พร้อม Username, Host และ Current Directory

$ export PS1="\e[0;34m\u@\h \w> \e[m"  [Note: สำหรับ Light Blue Prompt]
$ export PS1="\e[1;34m\u@\h \w> \e[m"  [Note: สำหรับ Dark Blue Prompt]
  • \e[ – เริ่มต้น Color Prompt
  • x;ym – Color Code ใช้ค่า Color Code ตามตารางด้านล่าง
  • \e[m – จบ Color Prompt

ตาราง Color Code:

Black 0;30    Blue 0;34    Green 0;32    Cyan 0;36
Red 0;31      Purple 0;35  Brown 0;33
[Note: เปลี่ยน 0 เป็น 1 สำหรับ Dark Color]

ทำให้การเปลี่ยนสีเป็น Permanent โดยเพิ่มบรรทัดต่อไปนี้ใน .bash_profile หรือ .bashrc

STARTCOLOR='\e[0;34m'; ENDCOLOR="\e[0m"
export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"

5. เปลี่ยนสี Background ของ Prompt

เปลี่ยน Background Color โดยระบุ \e[{code}m ใน PS1 Prompt

$ export PS1="\e[47m\u@\h \w> \e[m" [Note: สำหรับ Light Gray Background]

รวม Background และ Foreground

export PS1="\e[0;34m\e[47m\u@\h \w> \e[m" [Note: Light Blue Foreground และ Light Gray Background]

เพิ่มใน .bash_profile หรือ .bashrc เพื่อทำให้ Permanent:

STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m"
ENDCOLOR="\e[0m"
export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"

ลองเล่นกับ Background Color ต่อไปนี้ และเลือกที่ชอบ:

  • \e[40m
  • \e[41m
  • \e[42m
  • \e[43m
  • \e[44m
  • \e[45m
  • \e[46m
  • \e[47m

6. แสดงหลายสีใน Prompt เดียว

คุณสามารถแสดงหลายสีใน Prompt เดียวกันได้ เพิ่ม Function ต่อไปนี้ใน .bash_profile

function prompt {
  local BLUE="\[\033[0;34m\]"
  local DARK_BLUE="\[\033[1;34m\]"
  local RED="\[\033[0;31m\]"
  local DARK_RED="\[\033[1;31m\]"
  local NO_COLOR="\[\033[0m\]"
  case $TERM in
    xterm*|rxvt*)
      TITLEBAR='\[\033]0;\u@\h:\w\007\]'
      ;;
    *)
      TITLEBAR=""
      ;;
  esac
  PS1="\u@\h [\t]> "
  PS1="${TITLEBAR}\ $BLUE\u@\h $RED[\t]>$NO_COLOR "
  PS2='continue-> '
  PS4='$0.$LINENO+ '
}

Re-login เพื่อให้มีผล หรือ Source .bash_profile:

$. ./.bash_profile
$ prompt
ramesh@dev-db [13:02:13]>

7. เปลี่ยนสี Prompt ด้วย tput

คุณสามารถเปลี่ยนสี PS1 Prompt ด้วย tput ได้:

$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"

tput Color Capabilities:

  • tput setab [1-7] – Set Background Color ด้วย ANSI Escape
  • tput setb [1-7] – Set Background Color
  • tput setaf [1-7] – Set Foreground Color ด้วย ANSI Escape
  • tput setf [1-7] – Set Foreground Color

tput Text Mode Capabilities:

  • tput bold – Set Bold Mode
  • tput dim – Half-bright Mode
  • tput smul – เริ่ม Underline Mode
  • tput rmul – ออกจาก Underline Mode
  • tput rev – Reverse Mode
  • tput smso – เข้า Standout Mode (Bold on rxvt)
  • tput rmso – ออก Standout Mode
  • tput sgr0 – ปิด Attributes ทั้งหมด

Color Code สำหรับ tput:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White

8. สร้าง Prompt ของตัวเองจาก Code ที่มีให้สำหรับ PS1

ใช้ Code ต่อไปนี้และสร้าง PS1 Linux Prompt ส่วนตัวที่ Functional และเข้ากับสไตล์ของคุณ Code ไหนจากรายการนี้จะช่วยชีวิตประจำวันได้มากที่สุด? คอมเมนต์บอกมาเลยว่า PS1 Code อะไรที่คุณใช้

  • \a ASCII Bell Character (07)
  • \d Date ในรูปแบบ "Weekday Month Date" (เช่น "Tue May 26")
  • \D{format} – Format ส่งให้ strftime(3) ผลลัพธ์แทรกใน Prompt String; Empty Format ให้ Locale-specific Time Representation ต้องมี Braces
  • \e ASCII Escape Character (033)
  • \h Hostname จนถึง Part แรก
  • \H Hostname เต็ม
  • \j จำนวน Jobs ที่ Shell จัดการอยู่
  • \l Basename ของ Terminal Device Name ของ Shell
  • \n Newline
  • \r Carriage Return
  • \s ชื่อ Shell, Basename ของ $0 (ส่วนหลัง Slash สุดท้าย)
  • \t เวลาปัจจุบัน 24-hour HH:MM:SS
  • \T เวลาปัจจุบัน 12-hour HH:MM:SS
  • \@ เวลาปัจจุบัน 12-hour am/pm
  • \A เวลาปัจจุบัน 24-hour HH:MM
  • \u Username ของ Current User
  • \v Version ของ Bash (เช่น 2.00)
  • \V Release ของ Bash, Version + Patch Level (เช่น 2.00.0)
  • \w Current Working Directory, $HOME ย่อด้วย Tilde
  • \W Basename ของ Current Working Directory, $HOME ย่อด้วย Tilde
  • \! History Number ของคำสั่งนี้
  • \# Command Number ของคำสั่งนี้
  • \$ ถ้า Effective UID เป็น 0 ให้ # มิฉะนั้น $
  • \nnn Character ตรงกับ Octal Number nnn
  • \\ Backslash
  • \[ เริ่ม Sequence ของ Non-printing Characters ใช้ Embed Terminal Control Sequence ใน Prompt
  • \] จบ Sequence ของ Non-printing Character

9. ใช้ Bash Shell Function ภายใน PS1 Variable

คุณสามารถเรียก Bash Shell Function ใน PS1 ได้:

ramesh@dev-db ~> function httpdcount {
> ps aux | grep httpd | grep -v grep | wc -l
> }
ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> "
ramesh@dev-db [12]> [Note: แสดงจำนวน Running httpd Processes ทั้งหมด]

เพิ่มบรรทัดต่อไปนี้ใน .bash_profile หรือ .bashrc เพื่อทำให้ Permanent:

function httpdcount {
  ps aux | grep httpd | grep -v grep | wc -l
}
export PS1='\u@\h [`httpdcount`]> '

10. ใช้ Shell Script ภายใน PS1 Variable

คุณสามารถเรียก Shell Script ภายใน PS1 Variable ได้ ในตัวอย่างด้านล่าง ~/bin/totalfilesize.sh ซึ่งคำนวณ Total Filesize ของ Current Directory ถูกเรียกใน PS1

ramesh@dev-db ~> cat ~/bin/totalfilesize.sh
for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
do
  let totalsize=$totalsize+$filesize
done
echo -n "$totalsize"
ramesh@dev-db ~> export PATH=$PATH:~/bin
ramesh@dev-db ~> export PS1="\u@\h [$(totalfilesize.sh) bytes]> "
ramesh@dev-db [534 bytes]> cd /etc/mail
ramesh@dev-db [167997 bytes]>
[Note: รัน totalfilesize.sh เพื่อแสดง Total File Size ของ Current Directory ใน PS1 Prompt] 

คุณปรับแต่ง PS1 ไปแล้วมากแค่ไหน? PS1 ของคุณเอาชนะ Angelina Jolie ได้ไหม? คอมเมนต์และ Share PS1 Prompt Value ของคุณ

Recommended Reading

Bash Cookbook, by Carl Albing, JP Vossen and Cameron Newham Bash เป็น Shell อันทรงพลัง หนังสือเล่มนี้จะช่วย Master Bash Shell และทำให้ Productive สูง ไม่ว่าคุณจะเป็น Sysadmin, DBA หรือ Developer ต้องเขียน Shell Script ที่จุดหนึ่ง Sysadmin ที่ฉลาดรู้ว่าเมื่อ Master Shell-scripting Techniques แล้ว จะให้ Servers ทำงาน Auto-pilot Mode ได้โดยให้ Shell-scripts ทำ Grunt Work เพื่อไปสู่ Auto-pilot Mode ของ Sysadmin คุณต้อง Master ตัวอย่างใน Cookbook นี้แน่นอน มี Bash Shell Books หลายเล่ม แต่มันนำหน้าทั้งหมดด้วย Detailed Examples มากมาย
 
Additional Linux book recommendation: 12 Amazing and Essential Linux Books To Enrich Your Brain and Library
 
If you liked this article, please bookmark it on del.icio.us and Stumble it.