I am viewing a ksh script and I see a function where the variable has been defined as below. Can anyone explain what exactly the below assignment of variable means in ksh script?
temprule="\$${APPLC_NM}"
temprule
will be assigned '$' followed by the value of the variable APPLC_NM
. So if APPLC_NM
is set to "pizza", temprule
will become "$pizza".
Note that temprule="\$$APPLC_NM"
would do the exact same thing. The brackets are only needed when the variable name is followed by a character that would be valid in a variable name.
As @Julie Pelletier
indicated, this is funny syntax to make a indirect variable, or a nameref. ksh
has some specialized syntax to make this work, however. This is a feature of ksh
, and might not work in other shells.
The more idiomatic way to write the same in ksh
would look like this:
# Set up the nameref: nameref temprule=APPLC_NM # Assign value to AAPLC_NM APPLC_NM=abc # The above two statements may be executed in any order. # Now, temprule will contain the value of APPLC_NM: echo $temprule abc
Now, no funny escaping of double $
is necessary, and the result is arguably more readable.