Upgrade 3CX to v18 and get it hosted free!

Asterisk cmd Set

Author image

Synopsis

Sets variable to value

Version differences:

This command is not available in Asterisk 1.0.9. Use SetVar instead.

  • As of v1.2 SetVar is deprecated and we are back to Set.
  • As of v1.4 the use of Set() to set multiple variables at once and the g flag have both been deprecated. Please use multiple Set() calls and the GLOBAL() dialplan function instead.
  • As of v1.6 only if you have a corresponding “compat” setting, then Set() does not strip surrounding quotes from the right hand side as it did previously.

Description

Set(variablename=value|options]) (starting with Asterisk 1.4)
Set(variablename=value[|variable2=value2][|options]) (up to and including Asterisk 1.2)

  • This application can be used to set the value of channel variables or dialplan functions. It will accept up to 24 name/value pairs upto Asterisk 1.2, but only one name/value pair in Asterisk 1.4 or later.
  • When setting variables, if the variable name is prefixed with _ the variable will be inherited into channels created from the current channel. If the variable name is prefixed with __ the variable will be inherited into channels created from the current channel and all children channels.
  • Next to the Set() command there is also the Asterisk func set available.

Options for Asterisk 1.2 (and possible earlier versions)

  • g: set a global variable (valid in the entire dialplan, not just the channel)

As of Asterisk 1.4 the correct syntax to set a global variable is like this

exten => 100,1,Set(GLOBAL(FOO)=456)

extensions.conf:

; If clearglobalvars is not set, then global variables will persist
; through reloads, and even if deleted from the extensions.conf or
; one if its included files, will remain set to the previous value.
;
clearglobalvars=no

Asterisk 1.6

Note that Set() changes behaviour in Asterisk 1.6 which can be controlled via asterisk.conf:

[compat]
app_set=1.6

If (and only if), in /etc/asterisk/asterisk.conf, you have a [compat] category, and you have app_set = 1.6 under that, then the behavior of this app changes, and does not strip surrounding quotes from the right hand side as it did previously.

Example

Set(numTries=4)
Set(CALLERID(number)=000000)
Set(CALLERID(name)="The Name")
Set(NIGHTMODE=1,g) ; set a global variable

To increment a variable, you can use:

Set(total=$[${total} + 1])

To set inherited variable:

Set(_CALLID=${UNIQUEID})
Set(__CALLID=${UNIQUEID})

Notes:

  • Variable names are not case sensitive.
  • Each channel gets its own variable space. There is no chance of collisions between different calls, and the variable is automatically trashed when the channel is hangup.
  • Make sure you do not put spaces around the equals sign in the assignment. Set(numTries = 4),with a space on either side of the “=”, will set numtries to “”.
  • If trying to zero out the CALLERID(name) do not use empty quotes, use Set(CALLERID(name)=)

Try using the variable in your dialplan:

Playback(${variablename})
SayDigits(${variablename})

See also


Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.