Chapter 58: Bash Functions

Bash Functions

This is one of the most beautiful and powerful parts of Bash scripting. Once you learn functions, your scripts stop being long messy lists of commands and start looking like clean, organized, reusable building blocks — just like how good programmers write code in any language.

Think of a function like a small reusable recipe card:

  • You write the recipe once (the function definition)
  • You give it a name
  • Later, whenever you need that recipe, you just say the name → the whole recipe runs automatically

So instead of repeating the same 10–15 lines of backup code in five different places, you write it once as a function called backup_folder, and then call it five times with different folder names.

What is a Bash Function? (Super Simple Definition)

A Bash function is:

  • A named block of code
  • That you can call (run) as many times as you want
  • Can take input (arguments)
  • Can return/output something
  • Lives inside your script (or in ~/.bashrc for global use)

Two Main Ways to Define a Function

Way 1: Most common & modern (recommended)

Bash

Way 2: Older style (still works everywhere)

Bash

Both are fine — most people use Way 1 now (with () after name).

Step-by-Step – Your First Function (Type This Right Now!)

Create a file:

Bash

Paste this:

Bash

Save → make executable → run:

Bash

You’ll see the greeting message three times — same code, run three times with one line each time: greet

That’s the magic!

2. Functions with Arguments (Input Parameters)

Functions become really useful when they accept arguments (like $1, $2 in scripts).

Bash

Run it — output:

text

local keyword → makes variable only exist inside the function (very good habit — prevents name clashes)

3. Function with Return Value (Output)

Bash functions don’t really “return” values like other languages — instead they:

  • Print output (most common)
  • Set exit status with return (0 = success, 1–255 = error)
Bash

$? after function call = the return value (or last command’s exit status)

4. Real-Life Useful Function Examples

Backup function (reusable)

Bash

Color message function

Bash

Summary Table – Bash Functions Cheat Sheet

Feature Syntax / Example Why it’s useful
Define function my_func() { … } Reusable block of code
Call function my_func or my_func arg1 arg2 Run it like a command
Arguments $1 $2 … $9 10″ {10} ” @” Pass data into function
Local variables local var=”value” Prevent name clashes
Return status return 0 (success) or return 1 (fail) Check with $? after call
Output (most common) echo “result” Capture with var=$(my_func)
Default argument value local dir=”${1:-~/default}” Flexible calls

Got it, boss? Functions are how your script becomes clean, organized, reusable, and professional — write once, call many times, pass arguments, get results.

Any part confusing? Want next:

  • “Teacher, explain how to return values properly (echo vs return)”
  • “Functions with arrays as arguments”
  • “Recursive functions (function calls itself)”
  • “Put functions in separate file & source them”
  • “Debug functions with set -x”

Just say — teacher is ready in Hyderabad! Start writing small functions in every script — even 3–4 lines — soon your code will look beautiful! 🐧🛠️🔧 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *