Technical skill

Bash syntax: functions

Give a command a reusable name

Bash is a program that reads commands typed at a prompt or saved in a script. A function is a set of commands with a name, so you can reuse the same small task without copying its steps everywhere. For example, a script can check whether a file is readable before trying to use it. A function runs when the script calls its name; simply defining it does not run its commands.

Here is a function that checks one path and reports success or failure:

check_readable() {
    if (($# != 1)); then
        printf 'usage: check_readable PATH\n' >&2
        return 2
    fi

    local path=$1
    if [[ ! -r "$path" ]]; then
        printf 'not readable: %s\n' "$path" >&2
        return 1
    fi
    return 0
}

The function expects one input. $# counts its inputs, and $1 is the first one. [[ ! -r "$path" ]] checks whether the current account can read that path. The local command makes path temporary to this function instead of overwriting a variable with the same name in the caller. Quotes around "$path" keep a filename with spaces together as one input. The >&2 part sends a message to standard error, the stream used for diagnostics.

Read the result separately from the message

A function returns an integer exit status. By convention, 0 means success and a nonzero number means failure. The status is not the same as printed text: text goes to standard output or standard error. If the function does not use return, Bash uses the status of its last command.

Call the example like this:

if check_readable /etc/hosts; then
    printf 'input is readable\n'
else
    printf 'check failed with status %s\n' "$?"
fi

The if checks the function's status. Inside the else branch, $? is that failed status. If another script needs a filename or count from a function, print it and have the caller capture standard output deliberately. Keeping error messages on standard error lets a caller redirect normal output without hiding the diagnostics.

Watch for changes to the caller

Functions run in the current Bash process. That means a function that runs cd changes the directory for the rest of the script; changing a shell option or a trap can also affect commands that run later. Make those side effects clear where the function is called. Use local variables for temporary values. Bash uses dynamic scoping for local variables: a nested function can see a local value from its caller, so choose names carefully when helpers call other helpers.

Bash function syntax and behavior apply when Bash interprets the script; another shell may not support them the same way. Check a script with bash -n or ShellCheck when available, then try a normal call and a failure case. Give each function a clear input and a useful status or output.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful