Advanced function techniques
In this section, we’ll explore some advanced techniques for working with Bash functions, including return values and recursive functions. We’ll provide code examples and thorough explanations to help you master these concepts.
Function return values
In Bash, functions don’t return values in the same way that functions in most programming languages do. Instead, they return an exit status, also known as a return code, which is an integer where 0
typically indicates success and any non-zero value indicates an error or some type of failure.
Returning an exit status
A Bash function returns an exit status using the return
command. By default, a Bash function will return the exit status of the last command executed within the function. Here’s a basic example (this code is provided in the book’s GitHub repository as ch05_exit_status.sh
):
function check_file { ls "$1" ...