Funktionen und Argumente in Shell-Skripten
Strukturieren Sie Ihre Skripte mit wiederverwendbaren Funktionen, übergeben Sie Argumente, geben Sie Werte zurück und verstehen Sie den Variablen-Scope in Bash.
Funktionen und Argumente in Shell-Skripten ist eine kostenlose Linux Command Line Mastery-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Linux Command Line Mastery-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Linux Command Line Mastery-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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'
}
greetPassing Arguments
Functions receive arguments positionally: $1, $2, and so on. Just like the whole script does.
greet() {
echo "Hello, $1"
}
greet AliceAll 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 threeReturn 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 evenReturning 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 4Functions Calling Functions
Functions can call each other, letting you compose small pieces into larger behavior.
double() { echo $(( $1 * 2 )); }
quad() { double $(double $1); }
quad 3Default Argument Values
Use parameter expansion to supply a fallback when an argument is missing.
greet() {
local name=${1:-World}
echo "Hello, $name"
}
greetA 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+$()= valueslocalkeeps variables scoped
Compose small functions and orchestrate them from main.
Häufig gestellte Fragen
Ist die Lektion „Funktionen und Argumente in Shell-Skripten“ kostenlos?
Ja — der vollständige Text von „Funktionen und Argumente in Shell-Skripten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Linux Command Line Mastery-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Linux Command Line Mastery-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Funktionen und Argumente in Shell-Skripten“?
Strukturieren Sie Ihre Skripte mit wiederverwendbaren Funktionen, übergeben Sie Argumente, geben Sie Werte zurück und verstehen Sie den Variablen-Scope in Bash. Du übst Linux Command Line Mastery mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Linux Command Line Mastery zu starten?
Keine Vorkenntnisse erforderlich. Linux Command Line Mastery auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Funktionen und Argumente in Shell-Skripten“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Linux Command Line Mastery-Lektion Code schreiben und ausführen?
Ja. Jede Linux Command Line Mastery-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Variablen, Eingabe und Ausgabe
- Bedingte Anweisungen: `if`, `else`, `case`
- Schleifen: `for`, `while`, `until`
- Funktionen und Argumente in Shell-Skripten