help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

name of a global variable to store the result of a function


From: Peng Yu
Subject: name of a global variable to store the result of a function
Date: Sat, 23 May 2020 18:25:24 -0500

As shown in the runtime comparison below, I'd like to return values
via a global variable instead via printing to stdout and $() in
certain situations.

Is there a well-accepted convention for the global variable to store the result?

$ cat ./main2.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

function f {
    echo 42
}

function g {
    result=42
}

TIMEFORMAT=%R
time for ((i=0;i<3000;++i)); do
    x=$(f)
done

time for ((i=0;i<3000;++i)); do
    g
    x=$result
done
$ ./main2.sh
4.181
0.048

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]