Linux Bash Define, Call Functions – POFTUT

Linux Bash Define, Call Functions

[rps-include post=6835]

Generally bash commands are executed regularly. So running complex or multiple commands one by one are difficult. Or sometimes we just want to accomplish task 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. Function has two way to create. Here are syntax for. Prefer second one because portability is important.

Define Function

Lets create a function that creates folder according to parameters.

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 .

Passing Arguments or Parameters To Function

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

And call the function named getParameters with parameters.

Passing Arguments or Parameters To Function

Passing Arguments or Parameters To Function

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

echo '

Return Values From Functions

In previous examples we have simply called the functions and run some statement 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 better way because we can use function result in different format. We can use echo statement in a function to end function and return given data to the caller.

LEARN MORE  What Is Shebang Linux or Bash Term?

In this example we will create a function named  mycat which will concatenate given two strings and return the result.

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.

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.

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 is the file that contains bash code to import and . is the operator used to import bash file.

LEARN MORE  Bash Command Basics

[rps-include post=6835]

Leave a Reply

Your email address will not be published. Required fields are marked *