Derrick Stolee <[email protected]> writes:
> way to do this loop. The top thing on my mind are the 'eval "echo X"'
> lines. If they start processes, then we can improve the performance.
> If not, then it may not be worth it.
Sigh.
Do you mean 'echo' run inside 'eval' is one extra process? In most
modern shells, it is a built-in and you need another process.
Do you mean 'eval' running anything is one extra process? Because
anything done inside eval must be visible to the shell running it,
e.g.
var=myvar; eval "$var=val"
would evaluate string 'myvar=val' inside that shell itself and it
must be able to update the value of $myvar, whatever it does must
not add any extra process.
The primary reason why the loop in question uses eval is to allow
the callers to pass $n in single-quote to have it interpolated
lazily.
message='message $n'
for n in 1 2 3
do
echo "$message"
eval "echo \"$message\""
done
Each iteration, the first line gives
message $n
which is the thing that gets passed to 'echo' in the second line, so
you'll see
message $n
message 1
message $n
message 2
message $n
message 3
as the result.