1

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}" 

    2 Answers 2

    1

    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.

      0

      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.

      1
      • 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.CommentedMay 6, 2016 at 17:51

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.