Repeated shell logic becomes easier to change when one named command owns it. A Bash function groups commands behind a callable name, receives arguments through positional parameters, and reports success or failure through its return status.
Arguments supplied to a function temporarily become $1, $2, and the remaining positional parameters while that function runs. Declaring a working variable with local keeps it from replacing a same-named variable in the surrounding script.
A regular-file check exposes both outcomes without external services or privileged setup. An existing file produces status 0, while a missing path produces status 1 that the caller can handle or pass back to the shell.
Related: How to create and run a Bash script
Related: How to use conditionals in Bash scripts
Related: How to source a function file in Bash
Steps to create a function in Bash:
- Create function-demo.sh with its interpreter line, unset-variable mode, and initial describe_path declaration.
- function-demo.sh
#!/usr/bin/env bash set -u describe_path() { local path=$1 }
The braces contain the function body. The local declaration copies the first function argument without changing a same-named variable outside the function.
- Insert the regular-file branch before the closing brace.
if [[ -f $path ]]; then printf 'regular file: %s\n' "$path" return 0 fi
return 0 stops the function with a successful status as soon as path names a regular file.
- Insert the missing-path branch after fi and before the closing brace.
printf 'not a regular file: %s\n' "$path" >&2 return 1
The diagnostic goes to standard error, and return 1 gives the caller a nonzero status it can test.
- Append the function call after the closing brace.
describe_path "${1:?Usage: bash function-demo.sh PATH}"The :? expansion stops the script with the usage message when no path argument is supplied. Because the function call is the script's final command, its return value also becomes the script's exit status.
- Compare function-demo.sh with the consolidated file.
- function-demo.sh
#!/usr/bin/env bash set -u describe_path() { local path=$1 if [[ -f $path ]]; then printf 'regular file: %s\n' "$path" return 0 fi printf 'not a regular file: %s\n' "$path" >&2 return 1 } describe_path "${1:?Usage: bash function-demo.sh PATH}"
- Check the completed script for Bash syntax errors.
$ bash -n function-demo.sh
No output means Bash parsed the file without finding a syntax error; it does not exercise either return path.
- Run the function with the script's own existing path.
$ bash function-demo.sh function-demo.sh regular file: function-demo.sh
- Run the function with a path that does not exist.
$ bash function-demo.sh missing.txt not a regular file: missing.txt
- Print the status from the missing-path run.
$ printf 'exit_status=%d\n' "$?" exit_status=1
Status 1 confirms that the function's failure value reached the calling shell.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.