Linux Bash Define and Call Functions – POFTUT

Linux Bash Define and Call Functions


Generally, bash commands are executed regularly. So running complex or multiple commands one by one is difficult. Or sometimes we just want to accomplish tasks with just one command without diving into it and distribute this command. Here are functions to make things very easy. Functions get parameters and execute functions code. A function has two ways to create. Here is syntax. Prefer the second one because portability is important.

function function_name {
  function_body
}
function_name () {
  function_body
}

Define Bash Function

Let’s create a function that creates a folder according to parameters.

function test {
        echo "Hello";
        echo "poftut.com";
}

As you see we used ; to make things more clear but omitting them do not hurts our code. Save this code as text file named test.sh .

Passing Arguments or Parameters To Function

Sometimes functions need parameters to use inside. Using parameters in function is very easy just to put parameters between parenthesis and make a function call. Inside function block use $1, $2, $3, … which is the parameter row numbers.

function getParameters(){ 
        echo $1; 
        echo $2; 
        echo $3; 
}

And call the function named getParameters with parameters.

$ . ./bash_function.sh
$ getParameters p1 p2 p3
Passing Arguments or Parameters To Function
Passing Arguments or Parameters To Function

As you see that at the first run we provided three parameters to the function and all of them printed but at the second run we only provided two parameters and the third parameter is printed as empty because it is not defined in function do echo $3; printed as an empty line.

Return Values From Functions

In previous examples, we have simply called the functions and run some statements in these functions like printf etc. The real power of function comes from its modularity. We can call a function and simply return some data. This is a better way because we can use function results in different formats. We can use echo statement in a function to end function and return given data to the caller. In this example, we will create a function named  mycat which will concatenate given two strings and return the result.

function mycat(){ 
        echo "$1 $2" 
}

Variable Scope

In bash a variable can be accessed from every where in the current session. This means each variable is by default global. A variable defined outside of the function can be accessed from inside function. This may create problems if we have same name variables in different scopes.

Local Variables

We can define variables inside function to make them only accessible from inside a function. This will also prevent collusion of same named variables inside and outside of the function. We can use local keyword to make variables local. In this example we will create variables named age outside and inside a function. We will set inside function age as local.

age=40 
 
function pr(){ 
        local age=20 
        echo $age 
} 
 
pr 
 
echo $age
Local Variables
Local Variables

As we from output first age variable inside function will print 20 and than outside function age variable prints 40 . If we remove the local from age variables we will get 20 for both variable prints.

LEARN MORE  How To Add New User Account To Linux

Source Code Blocks

Sourcing is a term used in bash that means specified bash script file is important in to current bash session. Thinks that you have implemented some function in a bash file and want to use them in current bash session only. We use syntax below where test.sh is the file that contains bash code to import and . is the operator used to import bash file.

$ . test.sh
$ getHostname 
lenovo.localhost

Leave a Comment