← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 3 of 13

Function Arguments

In this page:

  1. Function Arguments

Function Arguments

Functions receive arguments the same way scripts do: $1, $2, etc. for individual arguments, $# for the count, and $@ for all of them. There's no separate parameter-declaration syntax like in other languages — arguments are simply read positionally inside the function body. Function-local $1 shadows the script's own $1 while the function is running.

Note: Inside a function, $1 refers to the function's own first argument, not the script's.

Example: Function Arguments

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

greet "Alice" 30

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.