So i recently noticed that, when declaring a variable using a command substitution, let's say like so:
var=$(echo "this is running" > test; cat test)
Then it will run (the file test
will be created) , even though i didn't yet called it (technically), which i would like so:
var=$(echo "this is running" > test; cat test)
echo "$var" # is where i would "normally" call the variable
How could I prevent the command substitution from actually running when declaring it in a variable, so it only actually run when I call the said variable?
PS: Well aware this is kind of a bad example, but it serve well to demonstrate what i mean, although with "useless use of cat" and "useless use of echo"...
$SECONDS
,$RANDOM
,$PWD
... whose contents is automatically generated. You can also set theinteger
attribute on a variable which causes its contents to be interpreted as arithmetic expression (though in the case ofbash
, that's done upon assignment only after the attribute has been set; still, seea='b[$(uname>&2)]' bash -c 'var=$a; typeset -i var; var=var+1'
for instance).