While developing bash scripts, sooner or later you will come across the fact that you periodically have to use the same code fragments. Typing them by hand is boring all the time, and copying and pasting is not our method. How to be? It would be nice to find a tool that allows you to write a block of code once and, when you need it again, just refer to it in the script. The bash shell provides this capability by allowing you to create functions. Bash functions are named blocks of code that can be reused in scripts.

Image for post

Function declaration

The function can be declared like this:

functionName {
}

Or like this:

functionName() {
}

The function can be called with no arguments and with arguments.

Using functions

Let’s write a script that contains a function declaration and uses it:

#!/bin/bash
function myfunc {
echo "This is an example of using a function"
}
count=1
while [ $count -le 3 ]
do
myfunc
count=$(( $count + 1 ))
done
echo "This is the end of the loop"
myfunc
echo "End of the script"

A function named myfunc. To call a function, just specify its name.

raevskym@DESKTOP-JNF3L6H:~/bash_course$./myscript.sh
This is an example of using a function
This is an example of using a function
This is an example of using a function
This is the end of the loop
This is an example of using a function
End of the script

The function can be called as many times as needed. Please note that if you try to use the function before declaring it, you will encounter an error. Let’s write a script to demonstrate this:

#!/bin/bash
count=1
while [ $count -le 3 ]
do
myfunc
count=$(( $count + 1 ))
done
echo "This is the end of the loop"
function myfunc {
echo "This is an example of using a function"
}
echo "End of the script"

As expected, nothing good has happened since its launch.

raevskym@DESKTOP-JNF3L6H:~/bash_course$./myscript.sh
./myscript.sh: line 5: myfunc: command not found
./myscript.sh: line 5: myfunc: command not found
./myscript.sh: line 5: myfunc: command not found
This is the end of the loop
End of the script

When coming up with names for functions, keep in mind that they must be unique, otherwise, problems cannot be avoided. If you override a previously declared function, the new function will be called instead of the old one without any notification or error messages. Let’s demonstrate this with an example:

#!/bin/bash
function myfunc {
echo "The first function definition"
}
myfunc
function myfunc {
echo "The second function definition"
}
myfunc
echo "End of the script"

As you can see, the new function has quietly overwritten the old one.

raevskym@DESKTOP-JNF3L6H:~/bash_course$./myscript.sh
The first function definition
The second function definition
End of the script

Using the return command

The command returnallows you to specify the integer exit code returned by the function. There are two ways to work with what is the result of a function call. Here’s the first one:

#!/bin/bash
function myfunc {
read -p "Enter a value: " value
echo "adding value"
return $(( $value + 10 ))
}
myfunc
echo "The new value is $?"

The command echodisplayed the sum of the entered number and the number 10.

raevskym@DESKTOP-JNF3L6H:~/bash_course$./myscript.sh
Enter a value: 10
adding value
The new value is 20

The functionmyfuncadds 10 to the number contained in the variable$value, the value of which is set by the user while the script is running. Then it returns the result using the commandreturn. What the function returned is output by the commandechousing a variable$?.

If you execute any other command before extracting the$?function’s return value from the variable, that value will be lost. The point is that this variable stores the return code of the last executed command.

Note that the maximum number that the command can returnreturnis 255. If the function needs to return a larger number or string, a different approach is needed.

Writing the output of a function to a variable

Another way to return the results of a function’s work is to write the data output by the function to a variable. This approach allows you to bypass the limitations of the command returnand return any data from the function. Let’s consider an example:

#!/bin/bash
function myfunc {
read -p "Enter a value: " value
echo $(( $value + 10 ))
}
result=$( myfunc)
echo "The value is $result"

This is what you get after calling this script.

raevskym@DESKTOP-JNF3L6H:~/bash_course$./myscript.sh
Enter a value: 10
The value is 20

Function arguments

Think of bash functions as small snippets of code that save you time and space by eliminating the need to constantly type or copy the same sets of commands. However, the capabilities of the functions are much wider. In particular, we are talking about passing arguments to them.

Functions can use standard positional parameters that store what is passed to them when called. For example, the name of a function is stored in a parameter $0, the first argument passed to it is in $1, the second in $2, and so on. The number of arguments passed to the function can be found by accessing the variable $#. If you are familiar with the third part, you can’t help but notice that this is all very similar to how scripts handle command line parameters passed to them.

#command-line #coding #programming #bash #linux #function

Functions and Library Development
1.20 GEEK