Asterisk Dialplan Variables, Functions and GotoIf
Learn Asterisk dialplan variables, _ and __ inheritance, $[ ] expressions, dialplan functions and GotoIf, with examples from the official docs.
By Arkay · Updated · Published

What is in our variables and functions reference?
We compiled this reference from the official Asterisk documentation, checked on November 14, 2026. Nothing here was tested. Every row comes from a docs page we checked, such as the Variables overview. The API pages were generated from Asterisk branch 23, and we show the version whenever the docs give one.
The sections below follow the questions readers ask most. Each one starts with the rule the docs state, then shows a short example.
How do I set and read a variable in the Asterisk dialplan?

Set(varname=value) sets a variable, and ${varname} reads it. The setting and substituting page shows the form exten => 1,2,Set(varname=value). In extensions.conf:
[default]
exten => 9,1,Set(COUNT=3)
exten => 9,2,NoOp(${COUNT})
The first step sets COUNT to 3. The second step reads it with ${COUNT}, which is replaced by 3 before NoOp runs. NoOp does nothing; it is there to help debugging.
The docs say you can substitute the value of a variable everywhere using ${variablename}. That includes the arguments of other applications, such as NoOp above.
Variable names are case-sensitive. COUNT, Count and count are three different variables, as the Variables overview explains. Reading one spelling does not return the value set under another, so pick one spelling and use it everywhere.
What is the difference between channel and global variables?
Asterisk has two types of dialplan variables: channel variables and global variables. The Variables overview describes both.
| Type | How it is set | How it is read |
|---|---|---|
| Channel variable | Set(name=value) | ${name} |
| Global variable | Declared in the [globals] section of extensions.conf, or set with Set(GLOBAL(name)=value) | ${name}, like any channel variable |
The two types differ in how they are set. A channel variable is set with Set(name=value). A global variable can be declared in [globals], or set from the dialplan with Set(GLOBAL(name)=value). Both types are read the same way, with ${name}.
In extensions.conf, a global can be declared and then set from the dialplan:
[globals]
MYGLOBALVAR=somevalue
[default]
exten => 9,1,Set(GLOBAL(MYGLOBALVAR)=somevalue)
exten => 9,2,NoOp(${MYGLOBALVAR})
The GLOBAL() page says the function gets or sets a global variable. It has been available since Asterisk 1.4.0. GLOBAL_DELETE and GLOBAL_EXISTS work in the same family.
How do _ and __ variable prefixes work for inheritance?
One underscore passes a variable to one child channel only. Two underscores pass it to every channel created from the new channel, indefinitely. The variable inheritance page describes both rules.
The docs say that with one underscore, the variable is not inherited any further than a single level, that is one child channel. With two underscores, any channels created by the new channel also receive the variable, and it keeps passing down indefinitely.
In extensions.conf:
[default]
exten => 9,1,Set(_FOO=bar)
exten => 9,2,Set(__BAR=baz)
The first step gives FOO to one child channel. The second step gives BAR to every channel created below it.
The prefix is not part of the name. _FOO, __FOO and FOO are the same variable, and setting any version removes the others. So after Set(_FOO=bar) and then Set(__FOO=baz), FOO passes to every descendant channel.
Which built-in channel variables can I read, and which are read-only?
Built-in variables marked with an asterisk in the docs can be read in the dialplan but not set. The docs call them builtin functions. Writes to them are silently ignored, so read the value back if a later step depends on it. The standard channel variables page lists them. These are the ones most often used:
| Variable | What it holds |
|---|---|
${EXTEN} | Current extension (read-only) |
${CONTEXT} | Current context (read-only) |
${CHANNEL} | Current channel name (read-only) |
${DIALSTATUS} | Result of Dial(): CHANUNAVAIL, CONGESTION, BUSY, NOANSWER, ANSWER, CANCEL, DONTCALL or TORTURE |
${CALLERID(num)} returns the caller ID number. It is a function, so it is covered in the functions section below.
You can also read part of a value with substring syntax. With NUMBER=98765, the results are:
| Expression | Result |
|---|---|
${NUMBER:2} | 765 |
${NUMBER:-2} | 65 |
${NUMBER:0:3} | 987 |
${NUMBER:1:3} | 876 |
The first number is the skip field, which strips that many digits from the front. A negative skip counts from the end, so -2 keeps the last two digits. The third number is the length.
How do I do math or comparisons in the dialplan with $[ ]?
Anything inside $[ ] is evaluated as an expression, and extra spaces are ignored. The expressions page explains how. In extensions.conf:
[default]
exten => 9,1,Set(lala=$[1 + 2])
exten => 9,2,Set(koko=$[2 * ${lala}])
lala becomes 3, and koko becomes 6.
The operators are listed below from lowest to highest precedence. Parentheses group as usual.
| Operator | Result |
|---|---|
a | b | a, unless it is empty or zero; otherwise b |
a & b | a, if neither a nor b is empty or zero; otherwise 0 |
= > >= < <= != | 1 or 0. Numeric if both sides are numbers, otherwise a string comparison |
+ - * / % | Sum, difference, product, quotient and remainder |
- and ! (unary) | Negation; ! returns 1 for null, 0, empty or "0", and 0 otherwise |
a : b | Regex match anchored at the start of the string |
a =~ b | Regex match, not anchored |
a ? b :: c | Conditional: b if a is true, otherwise c |
a ~~ b | Concatenates a and b into one string |
The comparison operators return 1 when the relation is true and 0 when it is false. You can use that result in a later Set() or GotoIf(). The % operator returns the remainder of a division. The operators page notes that precedence follows C-style rules.
What are dialplan functions and how do I call them?
Functions are read inside ${...}. For example, ${LEN(example)} gives 7. Set can also write to some functions, because the Set page says it sets channel variables or dialplan functions.
| Function | What it does | Since |
|---|---|---|
LEN(string) | Returns the length of a string | 1.2.0 |
IF(expression?true:false) | Returns the data after ? if true, otherwise the data after : | 1.2.0 |
CALLERID(datatype[,CID]) | Gets or sets caller ID data. Datatypes include all, name, num, pres, tag, ANI-num, DNID and RDNIS | 1.2.0 |
CUT(varname,char-delim,range-spec) | Slices a string by a delimiter, which defaults to a hyphen. Fields are numbered from 1 | 1.2.0 |
REGEX("regular expression" string) | Returns 1 on a match and 0 otherwise | 1.2.0 |
GLOBAL(varname) | Gets or sets a global variable | 1.4.0 |
DB(family/key) | Reads or writes the Asterisk database (AstDB) | 1.2.0 |
DB_DELETE(family/key) | Returns a value and removes the key | 1.4.0 |
CUT needs care. Its varname must be a plain variable name, such as MYVAR, not an expression such as ${MYVAR}. The CUT page states this rule. Ranges in the range-spec use a hyphen, groups use an ampersand, and fields are numbered from 1.
CALLERID takes a datatype and an optional CID argument. The docs say it gets or sets caller ID data on the channel. REGEX returns 1 on a match and 0 otherwise, so it can serve as a condition in GotoIf, as the advanced examples show.
How does GotoIf work and what happens if a label is left out?

GotoIf takes a condition and two labels: GotoIf(condition?labeliftrue:labeliffalse). An empty string or "0" is false, and anything else is true. The GotoIf page describes the labels, which use the Goto form [[context,]extension,]priority. In extensions.conf:
[default]
exten => 9,1,GotoIf($[${CALLERID(all)} = 123456]?2,1:3,1)
exten => 2,1,NoOp(Caller matched)
exten => 3,1,NoOp(Caller did not match)
When the condition is true, the call goes to extension 2. When it is false, the call goes to extension 3.
The docs say a condition is a string, and it is meant to be used with $[ ] expressions. The labels use the same syntax as the Goto application, which jumps to a priority, extension or context.
If the label for the outcome is left out, no jump happens and the next step runs. For example, ?2,1: with nothing after the colon falls through to the next step when the condition is false. If the target does not exist, Asterisk tries extension i, then h, and hangs up the channel if neither exists.
How do I store values across calls with the AstDB functions?
DB(family/key) reads and writes the Asterisk database, called AstDB. A read also sets DB_RESULT, as the DB page explains. In extensions.conf:
[default]
exten => 9,1,Set(DB(lastcaller/sales)=${CALLERID(num)})
exten => 9,2,NoOp(${DB(lastcaller/sales)})
Step 1 writes the caller ID number under the family lastcaller and the key sales. Step 2 reads it back. The value stays in AstDB after the call ends, so the next call can read it. If a key does not exist, the read returns a blank value.
DB_DELETE(family/key) returns a value and removes the key. It has been available since Asterisk 1.4.0.
Advanced examples
The two examples below combine the pieces above. Each uses only documented syntax.
Caller ID check with REGEX and GotoIf. In extensions.conf:
[default]
exten => 9,1,Set(CALLER=${CALLERID(num)})
exten => 9,2,GotoIf(${REGEX("^1" ${CALLER})}?3,1:4,1)
exten => 3,1,NoOp(Caller number starts with 1)
exten => 3,2,Hangup()
exten => 4,1,NoOp(Other caller)
Step 1 copies the caller ID number into CALLER. Step 2 runs REGEX against that value. REGEX returns 1 when the pattern matches and 0 otherwise, so GotoIf sends matching calls to extension 3 and other calls to extension 4. The pattern ^1 is an example only, not a real number range to copy.
Returning-caller greeting with DB(), LEN() and IF(). In extensions.conf:
[default]
exten => 9,1,Set(SEEN=${DB(seen/${CALLERID(num)})})
exten => 9,2,Set(GREETING=${IF($[${LEN(${SEEN})} > 0]?Welcome back:Welcome)})
exten => 9,3,Set(DB(seen/${CALLERID(num)})=1)
exten => 9,4,NoOp(${GREETING})
Step 1 reads a key named after the caller ID number. For a first-time caller the key does not exist, so SEEN is blank. Step 2 uses LEN() to test whether SEEN has any characters, and IF() picks the greeting in one line. Step 3 stores a 1 for this caller, so their next call gets Welcome back. Step 4 shows the chosen greeting with NoOp, which does nothing and only helps debugging.
More guides in Open-Source PBX and SIP Servers.
Frequently asked questions
- How do I branch on time of day with GotoIfTime?
- GotoIfTime takes times, weekdays, month days and months, an optional timezone, and then the true and false labels. Its page describes it as a conditional goto based on the current time. The time format follows IFTIME and TESTTIME, which this reference does not cover.
- When should I use ExecIf or the IF() function instead of GotoIf?
- Use ExecIf to run one application only when a condition is true, without jumping to another step. Use IF() to pick one of two values inside a line, such as in a Set(). Use GotoIf when the call should jump to one of two labels.
Sources
- Variable names are case-sensitive: COUNT, Count and count are different variables. (docs.asterisk.org)
- Set(varname=value) sets a variable, for example exten => 1,2,Set(varname=value). (docs.asterisk.org)
- GLOBAL(varname) gets or sets a global variable (since Asterisk 1.4.0). (docs.asterisk.org)
- A variable prefixed with one underscore, Set(_FOO=bar), is inherited by one child channel (docs.asterisk.org)
- Built-in variables marked with * (such as ${EXTEN}, ${CONTEXT} and ${CHANNEL}) can only be (docs.asterisk.org)
- Anything inside $[ ] is evaluated as an expression; Set(lala=$[1 + 2]) then Set(koko=$[2 * (docs.asterisk.org)
- expr1 | expr2 returns expr1 unless it is empty or zero, otherwise expr2. (docs.asterisk.org)
- Set(name=value) can set channel variables or dialplan functions. (docs.asterisk.org)
- CUT(varname,char-delim,range-spec) slices a string by a delimiter (default '-'); varname m (docs.asterisk.org)
- GotoIf(condition?[labeliftrue:[labeliffalse]]) is a conditional goto whose labels use the (docs.asterisk.org)
- DB(family/key) reads or writes the Asterisk database; a read returns blank if the key does (docs.asterisk.org)