Interview Scripting (Bash, Groovy)

How do you write and call a function in Bash? [Basic]

Answer

A Bash function is a reusable block defined as name() { commands; } or function name { commands; }. I call it by writing its name followed by arguments, just like a command.

Technical explanation

Functions share the shell process with the caller, so they can read variables, set globals, and return statuses.

Use local variables inside functions to avoid accidental global state changes.

Functions improve readability when a script has validation, logging, cleanup, retries, and separate action steps.

Hands-on example

log() { printf '[%s] %s\n' "$(date +%F_%T)" "$*"}

deploy() { local env="$1" log "deploying to $env"}

deploy prod

Preparing for an interview?

Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.

More Scripting (Bash, Groovy) interview questions

← All Scripting (Bash, Groovy) questions