Reputation: 945
I couldn't find what I am doing wrong in this script, I want the variable TimeVec to have all three time values. The script is supposed to save multiple running times of a program
#!/bin/bash
TIMEFORMAT=%R
TimeVec=""
for run in 1 2 3; do(
exec 3>&1 4>&2
foo=$( { time sleep 1 1>&3 2>&4; } 2>&1 )
exec 3>&- 4>&-
TimeVec+=$foo
echo $TimeVec
)
done
echo $TimeVec
this is what I get
1.001
1.003
1.003
EDIT
i would like the TimeVec variable to store all three time values.
Upvotes: 0
Views: 39
Reputation: 6995
You are running your loop in a subshell due to the parentheses in do ( ... ) done
. Each iteration gets its own subshell.
You want to use TimeVec
in the parent shell, but the children processes cannot modify a variable in their parent process, and they cannot share the variable among them. So the variable is always null to begin with as seen by each iteration.
Upvotes: 1