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

ลำดับการโหลด .bash_profile .bashrc .bash_login .profile และ .bash_logout ของ Bash

บทความนี้จะอธิบายลำดับการ Execute ของไฟล์ต่อไปนี้:

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • ~/.bash_login
  • ~/.profile
  • ~/.bash_logout

ลำดับการ Execute สำหรับ Interactive Login Shell

Pseudo Code ต่อไปนี้อธิบายลำดับการ Execute ของไฟล์เหล่านี้

execute /etc/profile
IF ~/.bash_profile exists THEN
  execute ~/.bash_profile
ELSE IF ~/.bash_login exist THEN
  execute ~/.bash_login
ELSE IF ~/.profile exist THEN
  execute ~/.profile
END IF
END IF
END IF

เมื่อ Logout จาก Interactive Shell ลำดับการ Execute คือ:

IF ~/.bash_logout exists THEN
  execute ~/.bash_logout
END IF

โปรดทราบว่า /etc/bashrc ถูก Execute โดย ~/.bashrc ตามตัวอย่างด้านล่าง:

# cat ~/.bashrc
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

ลำดับการ Execute สำหรับ Interactive Non-login Shell

ขณะ Launch Non-login Interactive Shell ลำดับการ Execute คือ:

IF ~/.bashrc exists THEN
  execute ~/.bashrc
END IF

หมายเหตุ: เมื่อ Non-interactive Shell เริ่มต้น จะมองหา Environment Variable ENV และ Execute ไฟล์ที่ระบุในค่า ENV Variable

ทดสอบลำดับการ Execute

วิธีทดสอบลำดับการ Execute หนึ่งคือ เพิ่มค่า PS1 ที่ต่างกันในไฟล์เหล่านี้ และ Re-login เข้า Shell ดูว่า PS1 ไหนถูกนำมาใช้โดย Linux Prompt เราเคยพูดถึงการใช้ PS1 ทำให้ Linux Prompt ทั้ง Functional และ Stylish

1. /etc/profile ถูก Execute เพิ่มบรรทัด PS1 ต่อไปนี้ใน /etc/profile และ Re-login เพื่อให้แน่ใจว่า Linux Prompt เปลี่ยนเป็นค่า PS1 ที่ตั้งใน /etc/profile

# grep PS1 /etc/profile
PS1="/etc/profile> "
[Note: re-login to see the prompt change as shown below]
Last login: Sat Sep 27 16:43:57 2008 from 192.168.1.2
/etc/profile>

ตรวจสอบให้แน่ใจว่า ~/.bash_profile ไม่มี PS1 เพื่อให้ข้างต้นทำงานได้ถูกต้อง

2. ~/.bash_profile ถูก Execute: เพิ่ม PS1 ต่อไปนี้ใน ~/.bash_profile, ~/.bash_login, ~/.profile และ ~/.bashrc Re-login เพื่อให้แน่ใจว่า Linux Prompt เปลี่ยนเป็นค่า PS1 ใน ~/.bash_profile

/etc/profile> grep PS1 ~/.bash_profile
export PS1="~/.bash_profile> "
/etc/profile> grep PS1 ~/.bash_login
export PS1="~/.bash_login> "
/etc/profile> grep PS1 ~/.profile
export PS1="~/.profile> "
/etc/profile> grep PS1 ~/.bashrc
export PS1="~/.bashrc> "
[Note: Upon re-login, it executed /etc/profile first and ~/.bash_profile next. So, it took the PS1 from ~/.bash_profile as shown below. It also did not execute ~/.bash_login, as ~/.bash_profile exists]
Last login: Sat Sep 27 16:48:11 2008 from 192.168.1.2
~/.bash_profile>

3. ~/.bash_login ถูก Execute. เปลี่ยนชื่อ .bash_profile เป็นอย่างอื่น Re-login เพื่อให้แน่ใจว่า Linux Prompt เปลี่ยนเป็นค่า PS1 ใน ~/.bash_login

~/.bash_profile> mv .bash_profile bash_profile_not_used
[Note: Upon re-login, it executed /etc/profile first. Since it cannot find ~/.bash_profile, it executed ~/.bash_login]
Last login: Sat Sep 27 16:50:55 2008 from 192.168.1.2
~/bash_login>

4. ~/.profile ถูก Execute. เปลี่ยนชื่อ .bash_login เป็นอย่างอื่น Re-login เพื่อให้แน่ใจว่า Linux Prompt เปลี่ยนเป็นค่า PS1 ใน ~/.profile

~/.bash_login> mv .bash_login bash_login_not_used
[Note: Upon re-login, it executed /etc/profile first. Since it cannot find ~/.bash_profile and ~/.bash_login, it executed ~/.profile]
Last login: Sat Sep 27 16:56:36 2008 from 192.168.1.2
~/.profile>

5. ~/.bashrc ถูก Execute สำหรับ Non-login Shell Testing การ Execute bash ที่ Command Prompt จะให้ Non-login Shell อีกอันหนึ่ง ซึ่งจะ Invoke .bashrc

~/.profile> bash
[Note: This displays PS1 from .bashrc as shown below.]
~/.bashrc> exit
exit
[Note: After exiting from non-login shell, we are back to login shell]
~/.profile>

หากชอบบทความนี้ โปรด Bookmark ที่ del.icio.us และ Stumble it.