Synopsis
Gets or sets the shared variable specifiedIntroduced with Asterisk 1.6.1
Description
SHARED(<varname>[,<channel>])
Implements a shared variable area, in which you may share variables between channels. If channel is unspecified, defaults to the current channel. Note that the channel name may be the complete name (i.e. SIP/12-abcd1234) or the prefix only (i.e. SIP/12).
The variables used in this space are separate from the general namespace of the channel and thus ${SHARED(foo)} and ${foo} represent two completely different variables, despite sharing the same name.
Finally, please realize that there is an inherent race between channels operating at the same time, fiddling with each others' internal variables, which is why this special variable namespace exists; it is to remind you that variables in the SHARED namespace may change at any time, without warning. You should therefore take special care to ensure that when using the SHARED namespace, you retrieve the variable and store it in a regular channel variable before using it in a set of calculations (or you might be surprised by the result).
Usage
On each channel, there is a space accessible for other channels to write:Set(SHARED(foo,SIP/123)=456)
or retrieve:
${SHARED(foo,SIP/123)}
The primary reason for having this space is writing out to another channel, since you can already import variables (and functions) from another channel, with the IMPORT function:
${IMPORT(SIP/123,CALLERID(name))}
Just remember that this is a special variable space and not the main variable space, so that other channels cannot mess with your execution except when you explicitly want them to be able to do so.
See also
- Asterisk cmd Set and Asterisk func SET
- Backport for Asterisk 1.4
- patch 1194: Initial discussion and patch
- CLI command "dialplan set chanvar" (or CLI command "core set chanvar" for asterisk < 1.6.2)
- Asterisk Manager command SetVar (AMI)
- Asterisk func IMPORT
- ImportVar
- function Asterisk func MASTER_CHANNEL of Asterisk 1.8: Retrieve and set variables on the channel which created the current channel
- Asterisk variables