Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

functions-bash

Functions in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Functions in Bash in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Functions in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

Functions group commands and accept positional parameters $1, $2, …—similar spirit to Python functions but without typed signatures.

Define and call

greet() {
  echo "Hello, $1"
}
greet "Ada"
greet "Lin"

$1 is the first argument; $@ is all arguments as separate words.

Return exit status

is_even() {
  local n=$1
  (( n % 2 == 0 ))
}
if is_even 4; then echo "even"; else echo "odd"; fi

Functions return the exit status of the last command—use return N explicitly when needed.

local variables

counter=0
bump() {
  local counter=1
  counter=$((counter + 1))
  echo "inside: $counter"
}
bump
echo "outside: $counter"

local avoids clobbering outer variables—good habit in larger scripts.

Important interview questions and answers

  1. Q: How do functions receive args?
    A: Positional parameters $1, $2, … and $@ for all.
  2. Q: What does local do?
    A: Creates a variable scoped to the function body.

Self-check

  1. What variable holds the first argument?
  2. How does a function signal success/failure without print?

Tip: Use local for temporaries—compare with Python local scope habits from Python.

Interview prep

How to pass args?

Positional $1, $2, ... and $@ for all.

local keyword?

Creates function-scoped variables.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • local vars?
  • return exit code?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump