Functions In Shell Script

Shell Functions are used to specify the blocks of commands that may be repeatedly invoked at different stages of execution.

The main advantages of using unix Shell Functions are to reuse the code and to test the code in a modular way.

The purpose of the function

  • Don't repeat yourself.
  • Write once, use many times.
  • Reduce the script length.
  • A single place to edit and troubleshoot.
  • Easier to maintain.
  • If you are repeating yourself, use a function.
  • Reusable code.
  • Must define before use.
  • Has parameters supports.
  • The best practice is to put all the functions on top of the script.

How to Create Function in Shell Script

In Shell script, we can write functions in a variety of different ways.

# type one.
function function_name(){
    # code goes here

# type two
    # code goes here
# method three
function function_name {
   # function code here.

Simply use the function name as a command to run a function.

# invode the function



# funtions
function install(){
  #### installations code.
  echo "installationscode1"
  # configurations code
  echo "configcode1"

function deploy {
  # deploy code.
  echo "deploy code 1"

let's run the above code and see the output.

└─$ ./
deploy code 1

Demo Video

Click Here for Demo Video