How to return two variables or more from a bash function?

Bash Liste Des Attaques Ovh


This article shows you how you can return 2 or more variables from a bash function.


Create your script

  • Within your shell (remotely mostly within putty)
touch learn-func-return-variables
chmod +x  learn-func-return-variables

Create your function and return two words

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:

  • takes an input stream or arguments as input
  • and return the output stream as output.
function return2Variables(){
   echo "value1 value2"


  • The echo will put a line of text composed of two words in the output stream of the function.

Get the output stream and creates two variables

The next step is to capture the function's output stream, and to create two variables from it.

  • Capture the output stream of the execution in a temporary variable
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"

Executing the script

The variable 1 value is: value1
The variable 2 value is: value2

Share this page:
Follow us:
Task Runner