...

/

Using Functions in Shell

Using Functions in Shell

Learn how to use functions in the shell mode of Bash.

Functions in shell

The functions are available in both shell and script execution Bash modes. First, let’s consider how they work in the shell.

Here is the general form of the function declaration:

FUNCTION_NAME()
{
  ACTION
}

We can also declare the function in one line this way:

FUNCTION_NAME() { ACTION ; }

The semicolon before the closing curly bracket is mandatory here.

The ACTION is a single command or block of commands. It is called the function body.

Function names follow the same restrictions as variable names in Bash. We are allowed to use Latin letters, numbers, and the underscore character there. However, the name must not begin with a number.

Declaring a function

...
Access this course and 1400+ top-rated courses and projects.