nested shell variables without using eval

后端 未结 3 1653
慢半拍i
慢半拍i 2021-01-12 01:42

Can I get rid of eval here? I\'m trying to set $current_database with the appropriate variable determined by user input (country and action)

3条回答
  •  孤街浪徒
    2021-01-12 02:15

    Actually, yes you can, and without resorting to associative arrays (which isn't a bad solution, mind you). You can use a solution similar to this:

    > current_database=$(echo final_${country}_${action}_path)
    > echo $current_database
    final_es_sales_path
    > current_database=${!current_database}
    > echo $current_database
    blahblah/es/sales.csv
    

    This avoids arrays and evals by using indirect expansion. This appears to have been introduced in the second version of Bash, so pretty much any machine should be able to do it.

提交回复
热议问题