Linux Bash Define, Call Functions

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-define-call-functions/
PINTEREST
PINTEREST
INSTAGRAM
  1. Introduction To Linux Bash Programming
  2. Linux Bash Directories and Files
  3. Linux Bash Environment Variables
  4. Linux Bash Variables
  5. Linux Bash Functions
  6. Linux Bash If Else Conditionals
  7. Linux Bash Create and Run Script Files
  8. Linux Bash Operators
  9. Linux Bash Arrays
  10. Linux Bash Input, Output and Redirection
  11. Linux Bash History
  12. Linux Bash Alias
  13. Linux Bash .bashrc File
  14. Linux Bash Debugging

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 test.sh .

Passing Arguments

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.

LEARN MORE  C Functions - Create and Run with Examples

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.

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.

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.

LEARN MORE  Linux Bash Alias and Shortcuts

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

  1. Introduction To Linux Bash Programming
  2. Linux Bash Directories and Files
  3. Linux Bash Environment Variables
  4. Linux Bash Variables
  5. Linux Bash Functions
  6. Linux Bash If Else Conditionals
  7. Linux Bash Create and Run Script Files
  8. Linux Bash Operators
  9. Linux Bash Arrays
  10. Linux Bash Input, Output and Redirection
  11. Linux Bash History
  12. Linux Bash Alias
  13. Linux Bash .bashrc File
  14. Linux Bash Debugging
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-define-call-functions/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.