How to create & use functions in shell scripts.

1.0K    Asked by RadhikaPatel in Devops , Asked on Feb 1, 2020
Answered by Radhika Patel

A function in shell scripting is written to divide entire code into small blocks or to dedicate a set of code for specific purpose just in the same way as it’s used in other programming languages like c, c++, python etc.

Function calling can be done, wherever required.

For e.g. - Let’s see below code for simple function calling.

#! /bin/bash

mytestfunc()

{

echo “Calling successfull”

}

###Main script

echo "Testing function calling"

mytestfunc



Below is the output -:

[root@localhost opt]# ./test.sh

testing function calling

“Hi this is my test function, tested OK ”

The above program shows and confirms function creation & calling.




Your Answer

Interviews

Parent Categories