0Pricing
Linux Command Line Mastery · レッスン

シェルスクリプトの関数と引数

再利用可能な関数でスクリプトを整理し、引数の受け渡しや戻り値、Bash における変数のスコープを学びます。

「シェルスクリプトの関数と引数」はCoddyKit上の無料Linux Command Line Masteryレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「シェルスクリプトの関数と引数」レッスンは無料ですか?

はい。「シェルスクリプトの関数と引数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Linux Command Line Masteryコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Command Line Masteryコースには全4レッスンが含まれています。

「シェルスクリプトの関数と引数」で何を学びますか?

再利用可能な関数でスクリプトを整理し、引数の受け渡しや戻り値、Bash における変数のスコープを学びます。 ブラウザで直接実行するハンズオンコードでLinux Command Line Masteryを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Linux Command Line Masteryを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのLinux Command Line Masteryは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン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に戻る