Variables
Description
RouterOS scripting language suports two types of variables, which are global (system wide) and
local (accessible only within the current script), respectively. A variable can be referenced by '$'
(dollar) sign followed by the name of the variable with the exception of set and unset commands
that take variable name without preceding dollar sign. Variable names should be composed of
contain letters, digits and '-' character. A variable must be declared prior to using it in scripts. There
are four types of declaration available:
• global - defined by global keyword, global variables can be accessed by all scripts and console
logins on the same router. However, global variables are not kept across reboots.
• local - defined by local keyword, local variables are not shared with any other script, other
instance of the same script or other console logins. The value of local variable value is lost
when script finishes.
• loop index variables - defined within for and foreach statements, these variables are used only
in do block of commands and are removed after command completes.
• monitor variables - some monitor commands that have do part can also introduce variables.
You can obtain a list of available variables by placing :environment print statement inside the
do block of commands.
You can assign a new value to variable using set action. It takes two unnamed parameters: the name
of the variable and the new value of the variable. If a variable is no longer needed, it's name can be
freed by :unset command. If you free local variable, it's value is lost. If you free global variable, it's
value is still kept in router, it just becomes inaccessible from current script.
Notes
Loop variables "shadows" already introduced variables with the same name.
Example
[admin@MikroTik] ip route> /
[admin@MikroTik] > :global g1 "this is global variable"
[admin@MikroTik] > :put $g1
this is global variable
[admin@MikroTik] >
Command Substitution and Return Values
Description
Some console commands are most useful if their output can be feed to other commands as an
argument value. In RouterOS console this is done by using the return values from commands.
Return values are not displayed on the screen. To get the return value from a command, it should be
enclosed in square brackets '[ ]'. Upon execution the return value of the the command will become
the value of these brackets. This is called command substitution.
Page 634 of 695
Copyright 1999-2007, MikroTik. All rights reserved. Mikrotik, RouterOS and RouterBOARD are trademarks of Mikrotikls SIA.
Other trademarks and registred trademarks mentioned herein are properties of their respective owners.