0Pricing
Linux Server Deployment & SSH Mastery · Lesson

Functions, Arguments, and Reusable Scripts

Make your Bash scripts modular and maintainable: define functions, pass and validate command-line arguments, return values, and structure scripts you can reuse across many server automation tasks.

Functions, Arguments, and Reusable Scripts is a free Linux Server Deployment & SSH 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 Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Scripts to Tools

You can write scripts, automate tasks, and handle errors. The next step is making scripts reusable: code organized into functions that accept arguments, so one script can serve many situations.

This is how throwaway scripts become real admin tools.

Defining a Function

A Bash function groups commands under a name. Define it once, call it many times. The body goes between braces.

greet() {
  echo 'Hello from the function!'
}

greet

Passing Arguments to Functions

Functions receive arguments positionally: $1, $2, and so on. $@ is all arguments, $# is the count.

greet() {
  echo "Hello, $1! You are $2 years old."
}

greet Alice 30

Script-Level Arguments

The same positional variables work for the whole script. Run ./deploy.sh staging v2 and the script reads $1 as 'staging' and $2 as 'v2'.

#!/bin/bash
ENV=$1
VERSION=$2
echo "Deploying $VERSION to $ENV"

Validating Arguments

Robust tools check their input. Verify the expected number of arguments and exit with a usage message if they are missing.

if [ $# -lt 2 ]; then
  echo "Usage: $0 <env> <version>"
  exit 1
fi

Returning Status from Functions

Functions return an exit status via return (0 = success). Callers check it with $? or directly in an if.

is_root() {
  [ "$(id -u)" -eq 0 ]
}

if is_root; then echo 'root'; else echo 'not root'; fi

Returning Data from Functions

To return actual data (not just status), echo it and capture with command substitution. The caller assigns the output to a variable.

timestamp() {
  date +%Y%m%d_%H%M%S
}

NOW=$(timestamp)
echo "Backup name: backup_$NOW"

Local Variables

By default Bash variables are global, which causes bugs across functions. Declare function-internal variables with local to keep them isolated.

add() {
  local result=$(( $1 + $2 ))
  echo $result
}

echo "Sum: $(add 3 4)"

Default Values and Named Flags

Provide sensible defaults with parameter expansion, and parse flags for friendlier tools.

  • ${1:-default} — use 'default' if $1 is unset
  • getopts — parse -v, -f file style flags
ENV=${1:-staging}
echo "Target environment: $ENV"

Sourcing a Library of Functions

Put shared functions in their own file and source it from multiple scripts. This avoids copy-pasting helpers everywhere.

# in lib.sh: define log(), is_root(), etc.
source ./lib.sh
log 'Starting deployment'

Best Practices

Write maintainable scripts:

  • Break logic into small, named functions
  • Always validate arguments and print usage
  • Use local for function variables
  • Share helpers via a sourced library file

Quick Check

Test your Bash function knowledge.

Recap

You can now build reusable Bash tools:

  • Define functions and call them repeatedly
  • Pass and validate arguments ($1, $@, $#)
  • Return status with return and data via echo + command substitution
  • Use local variables and source shared libraries

These patterns turn one-off scripts into a maintainable automation toolkit.

Frequently asked questions

Is the “Functions, Arguments, and Reusable Scripts” lesson free?

Yes — the full text of “Functions, Arguments, and Reusable Scripts” is free to read here on the web, and the Linux Server Deployment & SSH 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 Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.

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

Make your Bash scripts modular and maintainable: define functions, pass and validate command-line arguments, return values, and structure scripts you can reuse across many server automation tasks. You practise Linux Server Deployment & SSH 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 Server Deployment & SSH Mastery?

No prior experience is required. Linux Server Deployment & SSH 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, Arguments, and Reusable 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 Server Deployment & SSH Mastery lesson?

Yes. Every Linux Server Deployment & SSH 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. Introduction to Bash Scripting
  2. Automating Server Tasks
  3. Error Handling and Logging in Scripts
  4. Functions, Arguments, and Reusable Scripts
← Back to Linux Server Deployment & SSH Mastery