0Pricing
Linux Command Line Mastery · 课时

Shell 脚本中的函数与参数

使用可复用函数组织脚本、传递参数、返回值,并了解 Bash 中的变量作用域。

Shell 脚本中的函数与参数 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

常见问题解答

「Shell 脚本中的函数与参数」课时是免费的吗?

是的 — 「Shell 脚本中的函数与参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「Shell 脚本中的函数与参数」这节课中我会学到什么?

使用可复用函数组织脚本、传递参数、返回值,并了解 Bash 中的变量作用域。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Shell 脚本中的函数与参数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 变量、输入与输出
  2. 条件语句:`if`、`else`、`case`
  3. 循环:`for`、`while`、`until`
  4. Shell 脚本中的函数与参数
← 返回 Linux Command Line Mastery