This article shows you how you can return 2 or more variables from a bash function.
touch learn-func-return-variables
chmod +x learn-func-return-variables
#!/bin/bash
Bash is a wrapper language for commands. Therefore by default, it deals with standard streams (in a text form) that are the input and output of commands.
And each bash function behaves then also as a command that:
function return2Variables(){
echo "value1 value2"
}
where:
The next step is to capture the function's output stream, and to create two variables from it.
returnValue=$(return2Variables)
read -r variable1 variable2 <<< "$returnValue"
And you can use them as you wish:
echo "The variable 1 value is: $variable1"
echo "The variable 2 value is: $variable2"
./learn-func-return-variables
The variable 1 value is: value1
The variable 2 value is: value2