mervia@learn:~/workshops/terminal-10x/106$ ← /learn
· Pichaya Puttekulangkura พิชยะ พุฒิกุลางกูร · #terminal#beginners#macos
reading mode โหมดอ่าน

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 export changes 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 ~/.zshrc or 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 zsh by default
  • where ~/.zshrc fits into the everyday workflow
  • when ~/.zprofile is 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 ~/.zshrc does 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

ideacommand / filewhat it means
terminal app vs shellTerminal.app vs zshthe app opens a window; the shell is the command interpreter inside it
default interactive config~/.zshrcyour everyday zsh startup file
reload current shellsource ~/.zshrcre-read the file in the shell you already have open
shortcut a long commandalias gs='git status'give a command a shorter name

the practical rule is:

editing ~/.zshrc changes 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:

  1. leave the default alias as gs -> git status
  2. click write alias to ~/.zshrc
  3. click run alias
  4. notice that the file changed, but the current shell still says command not found
  5. click cat ~/.zshrc
  6. confirm the alias line is really saved on disk
  7. click source ~/.zshrc
  8. click run alias again
  9. click open new shell
  10. click run alias one more time

the teaching moment is steps 2 through 8:

  • saving config is not the same as loading config
  • source ~/.zshrc tells 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 ยังไม่รับค่าใหม่

ลองตามลำดับนี้:

  1. ปล่อย alias เริ่มต้นไว้ที่ gs -> git status
  2. กด write alias to ~/.zshrc
  3. กด run alias
  4. สังเกตว่าไฟล์เปลี่ยนแล้ว แต่ shell ปัจจุบันยังบอกว่า command not found
  5. กด cat ~/.zshrc
  6. ยืนยันว่า alias line ถูกบันทึกลงดิสก์แล้วจริง
  7. กด source ~/.zshrc
  8. กด run alias อีกรอบ
  9. กด open new shell
  10. กด run alias อีกครั้ง

หัวใจของบทนี้อยู่ระหว่างข้อ 2 ถึง 8:

  • การเซฟ config ไม่เท่ากับการทำให้ shell ใช้ config นั้นทันที
  • source ~/.zshrc คือการบอก shell ปัจจุบันให้อ่านไฟล์นั้นใหม่

พอภาพนี้ชัด การตั้งค่า shell จะเลิกดูเหมือนพิธีกรรมแล้วครับ

terminal 106 interactivelesson focus: a startup file can change on disk before the current shell notices

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.

$
Click a command card to run the guided path, or type the command yourself and press Enter.
alias being practicedalias gs='git status'Change it by typing a different echo "alias ..." command in the prompt.
current teaching momentWrite gs into ~/.zshrc first. Editing shell config changes the file on disk, not the current shell memory.
session 1 — zsh
# session 1: current shell loaded before you changed ~/.zshrc
Write an alias, try it too early, then reload the shell to make it available.
alias status

File state and shell state are separate on purpose

saved in ~/.zshrcnot saved yet
loaded in current shellno alias loaded yet
next shell startupwould start with no alias
dotfile snapshot

~/.zshrc

startup file read by new zsh sessions
# ~/.zshrc
lesson anchorEdit a file. Then reload the shell. Those are two different steps.

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:

~/.zshrc

for 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:

  • ~/.zshrc is a good default for aliases and interactive shell behavior
  • ~/.zprofile is 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 ~/.zshrc

tells 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:

  1. open ~/.zshrc in an editor (nano ~/.zshrc is enough; if the file does not exist yet, the editor can create it)
  2. add:
alias gs='git status'
  1. save the file
  2. run source ~/.zshrc
  3. 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 ของจริง ลองทำแบบนี้ครับ:

  1. เปิด ~/.zshrc ด้วย editor (nano ~/.zshrc ก็พอครับ ถ้าไฟล์ยังไม่มี editor จะสร้างให้ได้)
  2. เพิ่มบรรทัดนี้:
alias gs='git status'
  1. เซฟไฟล์
  2. รัน source ~/.zshrc
  3. รัน 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 ได้แล้ว

$ share

if this lesson helped, share the page with someone learning this topic. ถ้าโพสต์นี้มีประโยชน์ ส่งต่อหน้านี้ให้คนที่กำลังเรียนเรื่องนี้อยู่ได้เลย

share on facebook แชร์ไป Facebook

← back to all lessons ← กลับหน้าหลัก