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?


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
# The above two statements may be executed in any order.

# Now, temprule will contain the value of APPLC_NM:
echo $temprule

Now, no funny escaping of double $ is necessary, and the result is arguably more readable.

  • Your code does not prepend the variable contents with a '$' which is the result of OP's question. That probably means that the variable content is used as a variable name later on. – Julie Pelletier May 6 '16 at 17:51

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.