Passing parameters to a Bash function
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Bash functions receive parameters the same way shell scripts do: by position. That sounds simple, but the details around quoting, $@, shift, local variables, and return values matter a lot if you want reliable shell code. Most bugs in Bash functions are not caused by the function syntax itself. They come from argument handling mistakes.
Positional Parameters Work Just Like Script Arguments
Inside a Bash function, $1, $2, and so on refer to the arguments passed to that function call.
That prints a greeting using the first and second positional parameters. The special variable $# tells you how many arguments were passed.
This is the starting point for almost all Bash function parameter handling.
Use "$@" for All Arguments Safely
If a function needs to forward or iterate over all arguments, use "$@", not bare $@ and almost never "$*".
"$@" preserves argument boundaries correctly, including spaces. That is the safe default for shell functions.
By contrast, "$*" joins all arguments into one string, which is rarely what you want when forwarding parameters.
Use shift When Parsing Options
Functions that process a variable number of arguments often consume them step by step with shift.
This pattern is common in real scripts because it separates option parsing from remaining positional arguments.
Use Local Variables to Avoid Polluting Global State
Bash variables are global by default inside functions unless you mark them local.
Using local is a small discipline that prevents a lot of accidental interference between functions in larger shell scripts.
Bash Functions Return Status Codes, Not Arbitrary Values
A Bash function can return, but that return is only an exit status from 0 to 255. It is not how you return strings or large numbers.
If you want the function to produce a data value, print it and capture the output.
That distinction between status and output is fundamental in shell programming.
Validate Parameters Early
Because Bash is loosely typed, good functions validate what they need up front.
Failing early makes shell scripts much easier to debug, especially once they start calling other commands and functions transitively.
Common Pitfalls
- Forgetting to quote
"$@"and breaking arguments that contain spaces. - Using
returnwhen the function really needs to output a value. - Omitting
localand accidentally overwriting global shell variables. - Confusing
"$*"with"$@"when forwarding arguments. - Parsing options manually without
shift, leading to brittle argument handling.
Summary
- Bash function parameters are positional and accessed with
$1, $2, and so on. - Use
"$@"to handle all arguments safely. - Use
shiftfor option parsing and variable-length argument processing. - Use
localinside functions to avoid leaking state. - Remember that Bash functions return status codes, while data values are usually emitted via stdout.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.