Variables
In computer programming, variables are storage locations used to store information that can later be referenced and manipulated. You can consider a variable as a sort of a container where you store an information that can later be accessed using the name of the variable.
Variables are often used in the Asterisk dialplan since they can help us to reduce the clutter in the code and add logic. Four types of variables are available in Asterisk – global, shared, channel, and environment variables.
To set a variable to a particular value, we use the Set application. To later use a variable in the dialplan, we type a dollar sign, an opening curly brace, the name of the variable, and the closing curly brace.
Here is a simple example:
exten => 432,1,Set(JOHN=SIP/001565123123) same => n,Dial(${JOHN})
In the code above we’ve specified that, when the extension 432 is called, the variable JOHN will be set to the value of SIP/001565123123. In the second line, we’ve used the variable JOHN inside the Dial application. Asterisk will replace ${JOHN} with SIP/001565123123, so the line will look like this in the end:
same => n,Dial(SIP/001565123123)
Here is how the code above will be interpreted in the Asterisk CLI:
geek-university*CLI> == Using SIP RTP CoS mark 5 -- Executing [432@local:1] Set("SIP/bob-00000014", "JOHN=SIP/001565123123") in new stack -- Executing [432@local:2] Dial("SIP/bob-00000014", "SIP/001565123123") in new stack