Functions in shell scripting


In shell scripting, functions allow you to group a set of commands together and give them a name, making your code more modular, readable, and maintainable. Functions are a way to encapsulate code so that it can be easily reused and organized within your shell scripts.

Basic syntax and usage of functions in shell scripting:

Syntax:

function_name() {
    # Function code
    # ...
}
 

OR

function function_name {
    # Function code
    # ...
}
 

Usage:

  1. Declare the function by providing a name for it.
  2. Use { and } to enclose the code that defines the function.
  3. Inside the function, you can include any valid shell commands and statements.
  4. Call the function by its name.

Simple example of a function that greets a person:




Rs. 5.0 Rs. 10.0


Buy Now