Variable substitution with an exclamation mark in bash

I have the following lines in my .cfg bash script file

DDF_SOURCE="siebel_DATA_DATE_FORMAT"
DATA_DATE_FORMAT=${!DDF_SOURCE}

how is ${!DDF_SOURCE} evaluated? It would be !siebel_DATA_DATE_FORMAT, which doesn’t make sense to me.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That is an indirect expansion, documented in man bash section EXPANSION, subsection Parameter Expansion:

If the first character of parameter is an exclamation point (!), a
level of variable indirection is introduced. Bash uses the value of
the variable formed from the rest of parameter as the name of the
variable; this variable is then expanded and that value is used in the
rest of the substitution, rather than the value of parameter itself.
This is known as indirect expansion.

bash-4.2$ DDF_SOURCE="siebel_DATA_DATE_FORMAT"

bash-4.2$ siebel_DATA_DATE_FORMAT='Hello Indirect Redirection'

bash-4.2$ DATA_DATE_FORMAT=${!DDF_SOURCE} # siebel_DATA_DATE_FORMAT must get value before this line

bash-4.2$ echo $DATA_DATE_FORMAT
Hello Indirect Redirection


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x