0Pricing
Linux Command Line Mastery · Lesson

Functions and Arguments in Shell Scripts

Organize your scripts with reusable functions, pass arguments, return values, and understand variable scope in Bash.

Functions and Arguments in Shell Scripts is a free Linux Command Line Mastery lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Command Line Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Functions and Arguments in Shell Scripts” lesson free?

Yes — the full text of “Functions and Arguments in Shell Scripts” is free to read here on the web, and the Linux Command Line Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Command Line Mastery course, upgrade to CoddyKit PRO.

What will I learn in “Functions and Arguments in Shell Scripts”?

Organize your scripts with reusable functions, pass arguments, return values, and understand variable scope in Bash. You practise Linux Command Line Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Linux Command Line Mastery?

No prior experience is required. Linux Command Line Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Functions and Arguments in Shell Scripts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Linux Command Line Mastery lesson?

Yes. Every Linux Command Line Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Variables, Input, and Output
  2. Conditional Statements: `if`, `else`, `case`
  3. Loops: `for`, `while`, `until`
  4. Functions and Arguments in Shell Scripts
← Back to Linux Command Line Mastery