I have a shell script of which I want to capture the output using a block of code (docs).
Additionally I want to use the value of a variable from that block of code in the filename I write the output to, unfortunately that is not working.
How can I re-use the variable from a block of code in the filename?
Minimum example
This is a dummy script showing what I'm trying to do. I have GNU bash, version 5.0.17
:
{
v=1
echo $v
} >> output-$v.log 2>&1
I set a variable, run some command and want to store the stdout and stderr in a file that has the variable in it's name (output-1.log
in this case).
However, this writes out a file output-.log
as if the $v
variable does not exist.
What I've tried
If I add a semicolon ;
after the block of code:
{
v=1
echo $v
}; >> output-$v.log 2>&1
The file output-1.log
is created, but it remains empty