terminal 106: shell config on macOS terminal 106: shell config บน macOS
understand Terminal vs shell, edit `~/.zshrc`, reload with `source ~/.zshrc`, and add aliases that actually stick เข้าใจความต่างระหว่าง Terminal กับ shell, แก้ `~/.zshrc`, โหลดค่ากลับเข้ามาด้วย `source ~/.zshrc`, และเพิ่ม alias ให้ใช้งานได้จริงแบบไม่หาย
in terminal 105 you learned the rule underneath a lot of command-resolution problems:
- the shell searches PATH in order
- a temporary
exportchanges the current shell - and a new shell starts with its own startup state
106 is where you stop patching the current shell and start configuring the default one.
for modern macOS, that default shell is usually zsh.
if you’re on Windows, the specific files here (~/.zshrc, ~/.zprofile) are zsh/macOS. the underlying idea — “your shell reads startup files, and you have to reload them after editing” — still applies to PowerShell, just with a different file ($PROFILE) and reload step. skim for the mental model, then look up the PowerShell specifics separately.
the beginner problem here is not “how do i become a shell wizard?”
it’s much simpler:
where do i put everyday shell settings so they actually load, and what do i have to do after editing that file?
the answer for this lesson is:
- put aliases and everyday interactive shell config in
~/.zshrc - if a tool installer tells you to put PATH setup in
~/.zprofile, follow the tool’s instruction - reload it with
source ~/.zshrcor open a new shell
what you’ll know by the end
- the difference between Terminal and the shell running inside it
- that modern macOS usually starts
zshby default - where
~/.zshrcfits into the everyday workflow - when
~/.zprofileis the better file for login-time PATH setup - how to reload changes with
source ~/.zshrc - how to add and use a simple alias
- the footgun for this lesson: editing
~/.zshrcdoes not change the current shell until you reload it
what this assumes
terminal 101 through terminal 105.
if PATH and temporary export changes make sense now, you’re ready for the file that makes those changes stick.
4 ideas that make shell config feel normal
| idea | command / file | what it means |
|---|---|---|
| terminal app vs shell | Terminal.app vs zsh | the app opens a window; the shell is the command interpreter inside it |
| default interactive config | ~/.zshrc | your everyday zsh startup file |
| reload current shell | source ~/.zshrc | re-read the file in the shell you already have open |
| shortcut a long command | alias gs='git status' | give a command a shorter name |
the practical rule is:
editing
~/.zshrcchanges the file on disk first. your current shell only notices after reload or restart.
for this lesson, keep the scope narrow: aliases and everyday interactive tweaks belong in ~/.zshrc. if an installer like Homebrew tells you to add a PATH line to ~/.zprofile, follow that instruction instead of forcing everything into one file.
try it yourself
the interactive demo below focuses on exactly that stale-config moment.
work through this order:
- leave the default alias as
gs->git status - click
write alias to ~/.zshrc - click
run alias - notice that the file changed, but the current shell still says command not found
- click
cat ~/.zshrc - confirm the alias line is really saved on disk
- click
source ~/.zshrc - click
run aliasagain - click
open new shell - click
run aliasone more time
the teaching moment is steps 2 through 8:
- saving config is not the same as loading config
source ~/.zshrctells the current shell to read the file again
once that becomes obvious, shell setup stops feeling like superstition.
ใน terminal 105 เราเพิ่งได้กฎที่อยู่ใต้ปัญหาเรื่อง command resolution หลายแบบไปครับ:
- shell ค้น PATH ตามลำดับ
exportแบบชั่วคราวมีผลกับ shell ปัจจุบัน- shell ใหม่จะเริ่มจากค่าตั้งต้นของตัวเอง
106 คือจุดที่เราจะเลิกแก้เฉพาะ shell ตรงหน้าไปทีละรอบ แล้วเริ่มตั้งค่า shell หลักของเราให้ถูกทางแทน
สำหรับ macOS รุ่นปัจจุบัน shell เริ่มต้นมักจะเป็น zsh
ถ้าคุณใช้ Windows ไฟล์ที่พูดถึงในบทนี้ (~/.zshrc, ~/.zprofile) เป็นของ zsh/macOS ครับ แต่แนวคิดหลัก — “shell อ่าน startup file แล้วต้อง reload เองหลังแก้” — ใช้ได้กับ PowerShell เหมือนกัน แค่เปลี่ยนชื่อไฟล์เป็น $PROFILE และวิธี reload ต่างออกไปนิดหน่อย อ่านเพื่อจับภาพจำหลัก แล้วค่อยไปหา syntax ของ PowerShell แยก
ปัญหาของมือใหม่ในบทนี้ไม่ใช่ “จะเก่ง shell แบบสายลึกได้ยังไง?”
มันง่ายกว่านั้นมาก:
ถ้าอยากให้ค่าทั่วไปของ shell ใช้งานได้จริง ควรเก็บไว้ที่ไหน และหลังแก้ไฟล์แล้วต้องทำอะไรต่อ?
คำตอบของบทนี้คือ:
- เก็บ alias และค่าทั่วไปของ interactive shell ไว้ใน
~/.zshrc - ถ้า installer ของเครื่องมือบอกให้ใส่ PATH ลง
~/.zprofileก็ให้ทำตามที่เครื่องมือนั้นบอก - โหลดใหม่ด้วย
source ~/.zshrcหรือเปิด shell ใหม่
จบโพสต์นี้แล้วจะทำอะไรได้บ้าง
- แยกความต่างระหว่าง Terminal กับ shell ที่ทำงานอยู่ข้างในได้
- รู้ว่า macOS รุ่นปัจจุบันมักเริ่มด้วย
zsh - เข้าใจว่า
~/.zshrcอยู่ตรงไหนใน workflow ปกติ - รู้ว่าเมื่อไร
~/.zprofileเหมาะกว่าสำหรับ PATH ตอนเริ่ม shell - โหลดการเปลี่ยนแปลงกลับเข้ามาด้วย
source ~/.zshrc - เพิ่ม alias ง่ายๆ แล้วใช้งานได้จริง
- จำจุดพลาดสำคัญของบทนี้ได้: แก้
~/.zshrcแล้ว shell ปัจจุบันยังไม่เปลี่ยน จนกว่าจะโหลดใหม่
โพสต์นี้เริ่มจากไหน
ผ่าน terminal 101 ถึง terminal 105 มาแล้วจะต่อได้ลื่นครับ
ถ้า PATH และการเปลี่ยนแบบ export ชั่วคราวเริ่มชัดแล้ว ก็มาถึงไฟล์ที่จะทำให้สิ่งเหล่านั้นอยู่ถาวรได้อย่างถูกที่แล้วครับ
4 แนวคิดที่ทำให้ shell config ดูเป็นเรื่องปกติ
| แนวคิด | คำสั่ง / ไฟล์ | ความหมาย |
|---|---|---|
| แอป Terminal กับ shell ข้างใน | Terminal.app vs zsh | แอปคือหน้าต่าง ส่วน shell คือโปรแกรมตีความคำสั่งที่อยู่ข้างใน |
| ไฟล์ตั้งค่าหลักสำหรับการใช้งานทั่วไป | ~/.zshrc | ไฟล์ตั้งต้นของ zsh สำหรับการใช้งานประจำวัน |
| โหลด shell ปัจจุบันใหม่ | source ~/.zshrc | ให้ shell ที่เปิดอยู่กลับไปอ่านไฟล์ใหม่ |
| ทำทางลัดให้คำสั่งยาว | alias gs='git status' | ตั้งชื่อสั้นให้คำสั่งเดิม |
กฎที่ใช้ได้จริงมีประโยคเดียว:
การแก้
~/.zshrcคือการเปลี่ยนไฟล์บนดิสก์ก่อน ส่วน shell ปัจจุบันจะรู้ตัวก็ต่อเมื่อสั่งให้อ่านใหม่หรือเปิดใหม่
แต่ขอบเขตของบทนี้ขอให้แคบไว้ก่อนครับ: alias และการตั้งค่าทั่วไปของ interactive shell ให้อยู่ใน ~/.zshrc ส่วนถ้า installer อย่าง Homebrew บอกให้ใส่ PATH ลง ~/.zprofile ก็ให้ทำตามนั้น ไม่ต้องฝืนยัดทุกอย่างไว้ไฟล์เดียว
ลองเล่นดู
เดโมด้านล่างโฟกัสตรงช่วงที่ไฟล์ถูกแก้แล้ว แต่ shell ยังไม่รับค่าใหม่
ลองตามลำดับนี้:
- ปล่อย alias เริ่มต้นไว้ที่
gs->git status - กด
write alias to ~/.zshrc - กด
run alias - สังเกตว่าไฟล์เปลี่ยนแล้ว แต่ shell ปัจจุบันยังบอกว่า command not found
- กด
cat ~/.zshrc - ยืนยันว่า alias line ถูกบันทึกลงดิสก์แล้วจริง
- กด
source ~/.zshrc - กด
run aliasอีกรอบ - กด
open new shell - กด
run aliasอีกครั้ง
หัวใจของบทนี้อยู่ระหว่างข้อ 2 ถึง 8:
- การเซฟ config ไม่เท่ากับการทำให้ shell ใช้ config นั้นทันที
source ~/.zshrcคือการบอก shell ปัจจุบันให้อ่านไฟล์นั้นใหม่
พอภาพนี้ชัด การตั้งค่า shell จะเลิกดูเหมือนพิธีกรรมแล้วครับ
Alias playground
Save an alias into ~/.zshrc, watch the file update immediately, and then make the current shell catch up with source ~/.zshrc or a new shell window.
echo "alias ..." command in the prompt.File state and shell state are separate on purpose
~/.zshrc
Terminal vs shell
people mix these words constantly, so keep the simple version:
- Terminal is the app window
- shell is the program inside that reads and runs commands
on modern macOS, that shell is usually zsh.
this distinction matters because you are not editing “Terminal settings” when you add an alias. you are editing a shell startup file that zsh reads.
why ~/.zshrc is the right beginner default for aliases
you will eventually hear about other files like .zprofile, .bashrc, or .bash_profile.
for this series, that is noise.
the useful beginner default on modern macOS is:
~/.zshrcfor aliases, prompts, and small interactive tweaks, that keeps the lesson honest and actionable. you need one right place for everyday shell habits, not a survey of every startup-file edge case.
the important boundary is this:
~/.zshrcis a good default for aliases and interactive shell behavior~/.zprofileis commonly where macOS installers put login-time PATH setup
so if Homebrew or another tool gives you a specific file, trust the tool for that case.
what source ~/.zshrc actually does
this command:
source ~/.zshrctells the current shell:
“read this file again right now”
it does not open a new terminal window. it does not edit the file for you. it just makes the already-running shell load the current contents.
that is why it fixes the beginner moment of:
- “the alias line is clearly in the file”
- “but the alias still doesn’t work”
one real-world task
on your real Mac, try this:
- open
~/.zshrcin an editor (nano ~/.zshrcis enough; if the file does not exist yet, the editor can create it) - add:
alias gs='git status'- save the file
- run
source ~/.zshrc - run
gs
that is deliberately small. the goal is not to build a giant config file. the goal is to prove that you understand the saved-file vs loaded-shell difference.
once that clicks, adding PATH lines, aliases, and small quality-of-life tweaks becomes much less fragile.
common mistakes
you edit ~/.zshrc and expect the current shell to update itself. it will not. reload or open a new shell.
you confuse the Terminal app with the shell configuration inside it. different layers, different settings.
you paste config into random startup files because different blog posts say different things. for this stage, keep the alias lesson simple: use ~/.zshrc, and only switch files when a tool gives you a specific instruction.
you make permanent changes before testing the command once. keep the first alias tiny and easy to verify.
you treat aliases as magic instead of text substitution. an alias is just a shorter name that expands to another command.
official references
what this unlocks
you now have the core beginner setup model for the terminal:
- install tools
- understand PATH
- make temporary fixes
- move the real fix into shell config
- reload the shell and verify it
that is enough foundation to use the terminal with much less guesswork.
for the next step, use the terminal series hub to find the next post. the beginner foundation is now coherent enough to build on with applied-workflow posts.
Terminal กับ shell ต่างกันยังไง
สองคำนี้คนชอบพูดปนกันมากครับ เลยเอาเวอร์ชันที่ง่ายที่สุดไว้ก่อน:
- Terminal คือแอปหรือหน้าต่าง
- shell คือโปรแกรมข้างในที่อ่านและรันคำสั่ง
บน macOS รุ่นปัจจุบัน shell นั้นมักจะเป็น zsh
ความต่างนี้สำคัญ เพราะเวลาคุณเพิ่ม alias คุณไม่ได้กำลังแก้ “ตั้งค่า Terminal”
คุณกำลังแก้ไฟล์ startup ของ shell ที่ zsh จะอ่านต่างหาก
ทำไม ~/.zshrc ถึงเป็นค่าเริ่มต้นที่เหมาะกับมือใหม่สำหรับ alias
ต่อไปคุณจะได้ยินชื่อไฟล์อื่นอีก เช่น .zprofile, .bashrc, หรือ .bash_profile
แต่สำหรับซีรีส์นี้ นั่นคือเสียงรบกวนครับ
ค่าเริ่มต้นที่ใช้ได้จริงสำหรับมือใหม่บน macOS รุ่นปัจจุบันคือ:
~/.zshrcสำหรับ alias, prompt และการปรับ interactive shell เล็กๆ น้อยๆ แบบประจำวัน นี่คือจุดเริ่มต้นที่ตรงและใช้ได้จริง คุณต้องมี “ที่หลักสักที่” สำหรับนิสัยการใช้งานประจำ ไม่ใช่ต้องเริ่มจากการสำรวจ startup file ทุกแบบก่อน
ขอบเขตสำคัญมีแค่นี้ครับ:
~/.zshrcเป็นค่าเริ่มต้นที่ดีสำหรับ alias และพฤติกรรมของ interactive shell~/.zprofileเป็นไฟล์ที่ installer บน macOS มักใช้สำหรับ PATH ตอนเริ่ม session
ดังนั้นถ้า Homebrew หรือเครื่องมืออื่นบอกชื่อไฟล์มาเฉพาะ ก็เชื่อคำแนะนำนั้นในกรณีนั้นได้เลย
source ~/.zshrc ทำอะไรจริงๆ
คำสั่งนี้:
source ~/.zshrcกำลังบอก shell ปัจจุบันว่า:
“อ่านไฟล์นี้ใหม่ตอนนี้เลย”
มัน ไม่ได้ เปิดหน้าต่าง Terminal ใหม่ และ ไม่ได้ แก้ไฟล์แทนคุณ มันแค่ทำให้ shell ที่กำลังรันอยู่ โหลดเนื้อหาปัจจุบันของไฟล์เข้ามาใหม่
นี่จึงเป็นเหตุผลว่าทำไมมันถึงแก้ช่วงเวลาที่มือใหม่ชอบเจอว่า:
- “alias line ก็อยู่ในไฟล์ชัดๆ”
- “แต่ทำไม alias ยังใช้ไม่ได้”
โจทย์จากโลกจริง
บน Mac ของจริง ลองทำแบบนี้ครับ:
- เปิด
~/.zshrcด้วย editor (nano ~/.zshrcก็พอครับ ถ้าไฟล์ยังไม่มี editor จะสร้างให้ได้) - เพิ่มบรรทัดนี้:
alias gs='git status'- เซฟไฟล์
- รัน
source ~/.zshrc - รัน
gs
โจทย์นี้ตั้งใจให้เล็กครับ เป้าหมายไม่ใช่การสร้างไฟล์ config ยักษ์ เป้าหมายคือพิสูจน์ให้ได้ว่าคุณเข้าใจความต่างระหว่าง “ไฟล์ที่ถูกเซฟ” กับ “shell ที่โหลดค่าอยู่ตอนนี้”
พอจุดนี้ชัด การเพิ่ม PATH, alias, และการปรับเล็กๆ เพื่อให้ชีวิตง่ายขึ้นก็จะเปราะบางน้อยลงมากครับ
ข้อผิดพลาดที่พบบ่อย
แก้ ~/.zshrc แล้วคาดหวังว่า shell ปัจจุบันจะอัปเดตตัวเองทันที มันไม่ทำแบบนั้นครับ ต้องสั่งให้อ่านใหม่หรือเปิด shell ใหม่
สับสนระหว่างแอป Terminal กับ config ของ shell ข้างใน คนละชั้น คนละการตั้งค่า
คัดลอก config ลง startup file สุ่มๆ เพราะแต่ละบทความพูดไม่เหมือนกัน สำหรับช่วงนี้ ให้ยึดบทเรียนเรื่อง alias ให้ง่ายไว้ก่อน: ใช้ ~/.zshrc และค่อยเปลี่ยนไฟล์เมื่อเครื่องมือบอกมาเฉพาะ
ทำการเปลี่ยนถาวรเยอะเกินไปก่อนจะลองคำสั่งแรกให้สำเร็จสักครั้ง alias แรกควรเล็กและตรวจได้ง่าย
มอง alias เป็นเวทมนตร์แทนที่จะมองว่าเป็น text substitution alias ก็แค่ชื่อสั้นที่ขยายเป็นอีกคำสั่งหนึ่งครับ
อ้างอิงทางการ
เปิดประตูไปต่อที่ไหน
ตอนนี้คุณมีภาพจำแกนกลางของการตั้งค่า terminal สำหรับมือใหม่แล้วครับ:
- ติดตั้งเครื่องมือ
- เข้าใจ PATH
- แก้แบบชั่วคราว
- ย้ายวิธีแก้จริงไปไว้ใน shell config
- reload shell แล้วตรวจให้เห็น
แค่นี้ก็พอจะใช้ terminal แบบเดาน้อยลงมากแล้ว
ขั้นต่อไป ใช้ terminal series hub ดูว่ามีโพสต์ใหม่อะไรบ้างครับ เพราะ foundation ของมือใหม่ตอนนี้ต่อกันเป็นระบบ พร้อมต่อยอดไปโพสต์สาย applied workflow ได้แล้ว