bashscriptingscripting-language

Update value of a global variable with a variable inside in bash


How can I update a variable which has inside a variable and keep it global?

I know I can do this to update a variable but which isn't global:

for i in {1..24}; do 
    query=query_$i
    echo $query
done

I want to do something like this:

query=query_$i

for i in {1..24}; do
    echo $query
done

The variable doesn't update.


Solution

  • I don't really understand why you need to do this. If it's a quiz, one way I can think is to run query as a command inside "for" loop:

    query='echo query_$i'                                                                                                      
    
    for i in {1..24}; do                                                                                                       
        eval $query                                                                                                            
    done