HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Functions

Functions

Function definition and parameters ($1, $2).

Simple enough for a 5-year-old to understand

Function and parameters

Parameters are accessed with $1, $2 ...

Example code
1
greet() {
Function definition.
FunctionFunction definition.
2
echo "Hello $1"
$1 = first argument.
Output / Input$1 = first argument.
3
}
4
greet "Alex"
Call: Hello Alex
FunctionCall: Hello Alex
FunctionOutput / Input
💡 Hint: $1 is the first argument, $2 is the second; $@ is all, $# is the argument count.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write an addUp() function: it should add two arguments using $(( )) and print them; call addUp 8 9.

🎯 Expected output: 17

Loading editor…

About this topic

In Bash, a function is a named group of commands. Once defined, it is called just like a command, and arguments are passed separated by spaces—just like in commands—not inside parentheses.

Bash functions differ from functions in other languages in two important ways. First, they have no parameter names: arguments are accessed by number. Second, return does NOT return a value, it only sets an exit code, and this code must be between zero and two hundred fifty-five.

To return a value, the function prints the result to the screen, and the caller captures it using command substitution syntax. This is the standard and correct way in Bash; trying to return a value with return leads to silent errors.

What you will learn

  • Defining and calling a function
  • Accessing arguments by number
  • The relationship between return and the exit code
  • Printing output to return values
  • Defining local variables with local

When to use?

If the same group of commands is repeated in two places, it is moved to a function; the script length decreases and fixes are made in one place.

Every variable inside a function should be defined with local; otherwise, it becomes global and can silently corrupt the caller's variables.

Common mistakes

Wrong

add() {
  return $(($1 + $2))
}

add 3 4
echo "Result: $?"

return specifies an EXIT CODE, not a value, and the code must be between zero and two hundred fifty-five. With small numbers, this code seems to work, but for results greater than two hundred fifty-five, the value is silently wrapped: three hundred becomes forty-four.

Correct

add() {
  echo $(($1 + $2))
}

result=$(add 3 4)
echo "Result: $result"

Wrong

process() {
  count=0
  count=$((count + 1))
}

count=100
process
echo "$count"

Because the assignment inside the function is not local, it overwrites the GLOBAL variable: 1 is printed to the screen instead of 100. There is no error message, and finding such conflicts in large scripts is very difficult. Every variable inside a function must be defined with local.

Correct

process() {
  local count=0
  count=$((count + 1))
  echo "$count"
}

count=100
process
echo "$count"

Wrong

greet "Ali"

greet() {
  echo "Hello $1"
}

Bash reads the script from top to bottom; since the function is not defined yet, you get a "greet: command not found" error. Unlike many other languages, Bash has no concept of forward declaration. Functions must be defined before they are used.

Correct

greet() {
  echo "Hello $1"
}

greet "Ali"

Frequently asked questions

Should I write the function keyword?

It is not necessary, and omitting it is more portable: writing the name with empty parentheses is POSIX compliant, whereas the function keyword is Bash-specific. Using both together causes errors in some shells.

How do I know how many arguments were passed to the function?

A special variable holds the argument count. Checking mandatory arguments is a good habit: a function called with missing arguments works with empty values, and the error appears much later in a meaningless place.

Can I access the script's arguments inside a function?

No, the numbered variables inside a function refer to the FUNCTION'S arguments. If you need to access the script's own arguments inside a function, you must assign them to another variable beforehand or pass them explicitly to the function.

Summary

Function arguments are accessed by number; there are no parameter names.

return specifies an exit code, not a value, and is limited to two hundred fifty-five.

Output is printed to return a value, and the caller captures it.

Variables inside a function become global if they are not local.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code