Shell Scripting – Function
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed array in shell.
https://cloudaffaire.com/shell-scripting-array/
In this blog post, we will discuss functions in shell. Like any other programming language, bash also supports functions. A function is a block of code that performs certain actions and can be reused. You can define a function with a list of tasks and then call the function to perform those lists of tasks.
Shell Scripting – Function
Function Definition:
In order to use a function, you have to define the function first. In bash a function can be defined simply using function_name() {} or function function_name {}.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
################################ ## Shell Scripting | Function ## ################################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## Function Declaration myfun() { echo "hello world" } #or function myfun { echo "hello world" } |
Function Call:
In order to use the function, you need to call the function first. A function can be called simply using the function name. You can call a function anywhere you like for instance directly in bash, inside a bash script or inside another function as long as the function is available and is in the scope of the calling portion.
1 2 |
## Function call myfun ## returns hello world |
View Function Definition:
You can view the function definition using the inbuilt type.
1 2 |
## View function definition type myfun |
Function Deletion:
A function can be deleted using unset -f.
1 2 |
## Delete the function unset -f myfun |
Function Arguments:
You can pass arguments to the function during the function call. Function arguments can be accessed inside a function using $<argument_no>. $@ returns all the function arguments and $# returns the number of arguments passed in the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## Function Arguments myfun() { echo "Function Name: ${FUNCNAME[0]}" echo "Function No. Of Arguments: $#" echo "Function Arguments: $@" echo "Function First Argument: $1" } myfun 1 2 3 ## function call using 1 2 3 as arguments ## returns ## Function Name: myfun ## Function No. Of Arguments: 3 ## Function Arguments: 1 2 3 ## Function First Argument: 1 unset -f myfun ## remove the function |
Function Return:
Unlike traditional programming language, a bash function does not support a return value of a function. However, you can assign one integer value to the exit status of the function using return statement and then capture the exit status using $?.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Function Return myfun() { echo "hello world" return 10 ## only intergers supported } myfun ## call the funtion, returns only hello world echo $? ## get the return value of last executed function, returns 10 unset -f myfun ## remove the function |
Function Scope:
When you create a function, the function is only available to the session where it is created. If you want to access the function outside the session, you have to define the function in a file and source that file during runtime. Generally, when you call a function inside another function, the calling function can access all the variables from the source function. Function also supports a special type of variable local whose scope is within the function only to restrict this behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
## Function Scope cat myscript.sh -------------------- #!/bin/bash var1=10 F1() { var2=20 echo "hello from F1" echo "var2 = $var2" ## can access var2 } F2() { local var3=30 ## scope of local is only inside the function where it is defined echo "hello from F2" echo "var2 = $var2" ## can access var2 as F1 is called before F2 echo "var3 = $var3" ## can access var3 } MAIN() { F1 ## Function F1 called inside MAIN function F2 ## Function F2 called inside MAIN function echo "hello from MAIN" echo "var1 = $var1" ## can access var1 echo "var2 = $var2" ## can access var2 as F1 is called before MAIN echo "var3 = $var3" ## can not access var3 } MAIN ## Function MAIN called inside myscript.sh --------------------- ./myscript.sh ## execute the script ## returns ## hello from F1 ## var2 = 20 ## hello from F2 ## var2 = 20 ## var3 = 30 ## hello from MAIN ## var1 = 10 ## var2 = 20 ## var3 = echo $var1 $var2 $var3 ## can not access outside myscript.sh F1; F2; MAIN ## can not access outside myscript.sh cat ~/myfun.sh ---------------------- #!/bin/bash myfun() { echo "hello world" } ---------------------- source ~/myfun.sh ## immediate effect myfun ## function is available cat myscript.sh -------------------- #!/bin/bash . ~/myfun.sh ## include the function definition myfun ## call the function --------------------- ./myscript.sh ## and you can use it anywhere |
Hope you have enjoyed this article, in the next blog post, we will discuss shell expansion.