Functions in Shell Scripting - We have been learning Shell/Bash scripting and so far we have covered control flow (if-elif-else and case) statements and loops (for loop, while loop and select loop). This article is all about another important and widely used aspect of programming, called Functions.
A function is a block of code which has a name and performs a certain task. We can reuse this block of code by invoking it using the assigned name. Functions do not only improve readability of the program, but also reduce the redundancy in the code.
Syntax:
Or we can use a keyword
Lets take an example of a function named
Yay! You just created your first function. You can put any working code in the function body and give any meaningful name to your function.
Awesome! We just executed a block of code using a simple name! The story doesn't end here. We can provide data to the function, so that it can process the data by executing block of code on it and give us the output. Lets check this in more details.
Here, we have made two calls to the function
Lets take another example. We create a function
When we call this function with an alphanumeric string as an argument, lets see what the output looks like:
Thats it for the scope of this article. Stay tuned for more interesting ones.
A function is a block of code which has a name and performs a certain task. We can reuse this block of code by invoking it using the assigned name. Functions do not only improve readability of the program, but also reduce the redundancy in the code.
Defining a function
Its very easy to create a simple function. As said earlier, a function is a name given to a block of code and we enclose this block of code (or function body) between a pair of parenthesis. The basic syntax to create a function is as below:Syntax:
myFunction() { ... ... }
Or we can use a keyword
function
to define a function named myFunction
as shown below:function myFunction { ... ... }
Lets take an example of a function named
sayHello
that just has - echo "Hello world!"
in the function body.sayHello() { echo "Hello world!" } OR function sayHello { echo "Hello world!" }
Yay! You just created your first function. You can put any working code in the function body and give any meaningful name to your function.
Function call
Calling a function or simply a function call, is nothing but making use of the function you already created. To call a function, you must define a function. If you don't define a function, program will fail to execute giving our error. So, lets just call the function we just created -sayHello
.$ sayHello() { > echo 'Hello world!' > } $ sayHello Hello world!
Awesome! We just executed a block of code using a simple name! The story doesn't end here. We can provide data to the function, so that it can process the data by executing block of code on it and give us the output. Lets check this in more details.
Passing arguments to a function
In our article on Environment variables and special shell variables, we studied about positional arguments to the script. The case is exactly same here. We can make a function call, provide arguments to it and access those arguments using shell variables $1, $2, $3 and so on. In this case, $1 is the first argument to the function, $2 is the second argument the function and so on. Lets check how it works.$ sayHello() { > NAME=$1 > echo "Hello, $NAME" > } $ sayHello Dexter Hello, Dexter $ sayHello Debra Hello, Debra
Here, we have made two calls to the function
sayHello
with two different arguments Dexter
and Debra
. Each time a function call is made, NAME=$1
stores the first argument to the function (Dexter and Debra in two different function calls) to variable defined as NAME
. Once it is stored, we can use this variable as many times as we wish to.Lets take another example. We create a function
magicString
wherein it takes an alphanumeric string as an argument and tells you how many alphabets and numerics the string has.$ magicString() { STRING=$1 ALPHA=`echo $STRING | tr -d '[0-9]' | wc -c` NUM=`echo $STRING | tr -d '[a-zA-Z]' | wc -c` echo "Alphabets = $ALPHA; Numerics = $NUM" }
When we call this function with an alphanumeric string as an argument, lets see what the output looks like:
$ magicString su9Lei6aQuoh3gik Alphabets = 13; Numerics = 3 $ magicString Phe8aebe1uf69ac0 Alphabets = 11; Numerics = 5
Thats it for the scope of this article. Stay tuned for more interesting ones.
0 comments:
Post a comment