Bash – set global variable AND use stdout from bash function

bashbash-scripting

Here's the code:

#! /bin/bash

function foo() {
  G1=123
  echo "ReturnVal"
}

RV="$(foo)"
echo "RV=$RV, G1=$G1"  # RV=ReturnVal, G1=

foo >/dev/null
echo "G1=$G1"  # G1=123

I want to execute the function, set global variable G1, AND capture the stdout of the function into a variable.

The first call fails to set the global variable because the function is executing in a subshell. But that's the canonical way to get stdout into a variable.

I realize the 2nd call to foo() is throwing away stdout. Writing it to the console is equally pointless for my purpose. But it illustrates that the function is capable of setting the global variable.

Note that any solution cannot use a temporary file on the filesystem. The function I'm actually trying to write is already dealing with temp files and their automatic cleanup; introducing another temp file is not an option.

Is there a way?

Best Answer

Are you coming from Java, C++? That doesn't look any BASH I've ever seen.

#!/bin/bash

myFunction()
{
  echo eval "
  G1=\"123\";
  echo \"My function \$G1\"; "
}

myVar=$(myFunction 2>&1);
source <(myFunction) 2>&1 >/dev/null;
echo "G1: $G1";
eval echo "myVar: $(grep -v eval <($myVar))"

Very messy because exporting variables from a subprocess to its parent isn't allowed.

G1: 123
myVar: My function 123
Related Question