nested shell variables without using eval

后端 未结 3 1658
慢半拍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:35

    Doesn't

    current_database=${final_${country}_${action}_path}
    

    do what you want?

    Edit: No, it does not. Parameter expansion works only on one word (for the parameter name), and $ is not allowed in a word. It would be possible to use nested parameter expansion in the other parts of the more complicated versions (with limits, replacement, default value etc.), though, which is why the several expansion variants are listed here (which fooled me first) (emphasis by me):

    When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

    Sorry. Looks like eval and arrays are your best bet, then.

提交回复
热议问题