Created
December 31, 2019 12:09
-
-
Save aakbar5/9ac8073f30965c6d138b041dfc4d69a8 to your computer and use it in GitHub Desktop.
Bash function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple function which takes | |
# no parameters and returns nothing | |
function fun1() { | |
echo "A simple function" | |
} | |
# A function which takes parameters | |
# and return nothing | |
function fun2() { | |
echo "Fun:: Number of parameters: ${#@}" | |
echo "Fun:: List of all parameters: $@" | |
echo "Fun:: Parmeter # 1: $1" | |
echo "Fun:: Parmeter # 2: $2" | |
} | |
# A function which takes parameters | |
# and return a value using return | |
function fun3() { | |
local var1=$1 | |
local var2=$2 | |
local var3=$((var1 + var2)) | |
return $var3 | |
} | |
# A function which takes parameters | |
# and return a value using echo | |
function fun4() { | |
local var1=$1 | |
local var2=$2 | |
local var3=$((var1 + var2)) | |
echo $var3 | |
} | |
# | |
# Testing of functions | |
# | |
fun1 | |
fun2 10 20 | |
fun3 10 20 | |
echo "fun3 result = $?" | |
ret=$(fun4 10 20) | |
echo "fun3 result = $ret" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment