Search⌘ K
AI Features

Using Functions in Scripts

Explore how to declare and use functions within Bash scripts to simplify error handling and improve script maintainability. Learn techniques like nested function calls, managing variable scope, and supporting localization by converting error codes into user-friendly messages in different languages.

We'll cover the following...

We can declare a function in a script the same way as we do it in the shell. Bash allows both full or one-line form there. For example, let’s come back to the task of handling errors in the large program. We can declare the following function for printing error messages:

print_error()
{
  >&2 echo "The error has happened: $@"
}

This function expects input ...