0Pricing
Linux Command Line Mastery · บทเรียน

ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์

จัดระเบียบสคริปต์ด้วยฟังก์ชันที่นำกลับมาใช้ใหม่ได้ ส่งอาร์กิวเมนต์ คืนค่า และทำความเข้าใจขอบเขตของตัวแปรใน Bash

ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์ เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Functions?

You can write variables, conditionals, and loops, but as scripts grow they get repetitive. Functions let you name a block of logic and reuse it, keeping scripts clean and testable.

Defining a Function

A Bash function is a name followed by () and a body in braces. Define it before you call it.

greet() {
  echo 'Hello from a function'
}
greet

Passing Arguments

Functions receive arguments positionally: $1, $2, and so on. Just like the whole script does.

greet() {
  echo "Hello, $1"
}
greet Alice

All Arguments at Once

$@ expands to all arguments and $# gives their count, handy for variadic helpers.

show() {
  echo "count: $#"
  for a in "$@"; do echo "arg: $a"; done
}
show one two three

Return Status Codes

return sets an exit status (0-255), where 0 means success. Check it with $?.

is_even() {
  if [ $(( $1 % 2 )) -eq 0 ]; then return 0; else return 1; fi
}
is_even 4 && echo even

Returning Real Values

Since return only gives a status, output data with echo and capture it via command substitution.

square() {
  echo $(( $1 * $1 ))
}
result=$(square 5)
echo "result is $result"

Local Variables

By default Bash variables are global. Use local inside functions to avoid clobbering outer variables.

calc() {
  local total=0
  for n in "$@"; do total=$(( total + n )); done
  echo $total
}
calc 1 2 3 4

Functions Calling Functions

Functions can call each other, letting you compose small pieces into larger behavior.

double() { echo $(( $1 * 2 )); }
quad() { double $(double $1); }
quad 3

Default Argument Values

Use parameter expansion to supply a fallback when an argument is missing.

greet() {
  local name=${1:-World}
  echo "Hello, $name"
}
greet

A Practical Helper

Functions shine for reusable utilities like logging with a timestamp prefix.

log() {
  echo "[INFO] $*"
}
log 'Backup started'
log 'Backup finished'

Organizing Scripts

A clean pattern: define all functions at the top, then a main function at the bottom that orchestrates them. This mirrors structured programming.

main() {
  echo 'Running main'
}
main "$@"

Quick Check

How do you reliably get a computed value back from a Bash function?

Recap

Functions make scripts modular:

  • Define with name() { ... }
  • Arguments are $1, $@, $#
  • return = status; echo + $() = values
  • local keeps variables scoped

Compose small functions and orchestrate them from main.

คำถามที่พบบ่อย

บทเรียน “ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์”

จัดระเบียบสคริปต์ด้วยฟังก์ชันที่นำกลับมาใช้ใหม่ได้ ส่งอาร์กิวเมนต์ คืนค่า และทำความเข้าใจขอบเขตของตัวแปรใน Bash คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม

ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ตัวแปร อินพุต และเอาต์พุต
  2. คำสั่งเงื่อนไข: `if`, `else`, `case`
  3. ลูป: `for`, `while`, `until`
  4. ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์
← กลับไปที่ Linux Command Line Mastery