I need to be able to modify a string of a global (or static function) variable and in the meantime return it.
I can't find what I'm looking for online...
To make the question clearer, in Python3 will be something like this:
def get_static_variable():
if 'val' not in get_static_variable.__dict__:
# Some other code...
print('Some other code test') # Prints one time
get_static_variable.val = "Hello World!"
return get_static_variable.val
print('FUN: ' + get_static_variable())
print('FUN: ' + get_static_variable())
And this works correctly...
Some other code test
FUN: Hello World!
FUN: Hello World!
In bash I tried something like this:
#!/usr/bin/env bash
GLOBAL_VAR=""
function get_global_var(){
if [ -z "$GLOBAL_VAR" ]; then
# Some other code...
GLOBAL_VAR="Hello World!"
fi
echo $GLOBAL_VAR
}
echo "FUN: $(get_global_var)"
echo "VAR: ${GLOBAL_VAR}"
But as you can imagine, it doesn't work...
FUN: Hello World!
VAR: *empty*