bashshell

Bash: Store variables in string to get resolved at a later time


This is a small part of the code. I am creating test_args having a variable, which needs to be resolved at a later time, when that variable is defined in that scope, here, inside a for loop.

test_args=""
test_args+='-test.coverprofile=/tmp/coverage${ii}.out'
test_dirs="a b"

ii=0
for d in ${test_dirs[@]}; do
    echo ${test_args}               # actually used as `go test ${test_args}`
    ii=$((ii+1))
done

Current output:

-test.coverprofile=/tmp/coverage${ii}.out
-test.coverprofile=/tmp/coverage${ii}.out

Expected output:

-test.coverprofile=/tmp/coverage0.out
-test.coverprofile=/tmp/coverage1.out

I am open to suggestions for better methods. I need all the files to be created with a different name, so as to prevent overwriting in the loop.


Solution

  • Using eval is the usual solution:

    #!/bin/bash
    
    test_args=""
    test_args+='-test.coverprofile=/tmp/coverage${ii}.out'
    test_dirs="a b"
    
    ii=0
    for d in ${test_dirs[@]}; do
        eval echo ${test_args}
        ii=$((ii+1))
    done
    

    Which results in:

    [user@linux ~]$ ./test2.sh
    -test.coverprofile=/tmp/coverage0.out
    -test.coverprofile=/tmp/coverage1.out
    

    But here's a useful discussion about this topic - I advise reading the question linked on the left and it's answer, although it doesn't quite fully apply to this use case.