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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
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".
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.