Function in bash

dexter2305

Naveen

Posted on October 8, 2022

Function in bash

Function

Function is a familiar topic from mathematics to many programming languages. Function has a defined operation or computation, example: f(x)=x ^ 2.

Function, generally, takes one or more input, operates on the input and returns output. As in the example, given x, the function returns x ^ 2.

Function in bash

Bash scripts can have functions too - tricky part is to return the result of a computation. There are a few options to return the result of the computation in function back to the caller of the function.

Using return keyword

return, in bash, is used to status of execution. Status of execution is represented as integers ranging from 0 - 2 ^ 7 with 0 indicating and the rest indicating failure. The caller of such a function should use $? to read the returned value. This approach is restrictive that the result of computation may not always be returned with loss. Range 0-255 is too small range of integer probably my niece from kindergarten could use !

#!/bin/bash
function add() {
  return $(expr 200 + 200)
}

# main
add
echo $?
Enter fullscreen mode Exit fullscreen mode

Try this one out ! Expect 400 but result is going to be different (144). Clearly return is not meant return result of computation. It is meant to return the state of computation. 0 return code means and any other integer just means something went wrong in the computation.

Using global variable

Using global variable is a naive approach that leads mutation of state. Relying on the global variable based approach would nearly make the script stateful.

Using echo keyword

Using echo is open ended. Any arbitrary type can be returned, example Int, String. Caller of such a function should use evaluation to consume the output of computation. Unlike return statement, there is no limitation in the range, datatype and there is no mutation state.

#!/bin/bash
function add() {
   echo $(expr 200 + 200)
}

# main
sum=$(add 200 + 200)
echo $sum
Enter fullscreen mode Exit fullscreen mode

Using the echo approach is closer to how the commands are used within the script.

#!/bin/bash
today=$(date)
Enter fullscreen mode Exit fullscreen mode

Command date is a builtin tool and the example shows the usage. Now, I can relate to using echo more. I can build shell with all such library functions and import them into any necessary shell scripts.

Gotcha with echo

As the purpose of echo is only to return values, echo should not be used to perform console logging. However, logging to file is still fine with echo "My log message" > logfile

There is no one specific recommended approach. It depends on the scenario and the principles followed by the team. When the principles of referential transparency, purity has to be strictly followed, the preferred approach is to use echo approach.

💖 💪 🙅 🚩
dexter2305
Naveen

Posted on October 8, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related