Variables store data which your script can process or operate upon. In MaSH variables can be set or declared, assigned a specific value, cloned or destroyed. Variables can contain different types of data including strings (text), numbers, booleans (true or false), arrays and collections (lists of multiple values), dates, images and many more.
To define a variable in MaSH you use either the set keyword or the = sign like so:-
Natural
set myVariable to "Hello from mash"
Standard
myVariable = "Hello from mash"
We have declared a variable, named it myVariable and assigned it a string of text “Hello from mash” as its value. We can then use our variable in our script, for example to print it to the MaSH console:-
Natural
set myVariable to "Hello from mash"
printline myVariable
Standard
myVariable = "Hello from mash"
printline(myVariable)
Output
Hello from mash
The printline command is built in to MaSH and can be used to write data to the console or a log file.
As mentioned above we can store many types of data in our variables. Let’s declare a variable currentScore and set its value to 100:-
Natural
set currentScore to 100
printline currentScore
Standard
currentScore = 100
printline(currentScore)
Output
100
Just as before our variable is printed to the MaSH console.
When working with variables you can always change the data that they contain at any point in your script.
Natural
set currentScore to 100
printline currentScore
set currentScore to 200
printline currentScore
Standard
currentScore = 100
printline(currentScore)
currentScore = 200
printline(currentScore)
Output
100
200
As you can see the value of our currentScore variable has changed when we write it out a second time. What happened to the previous value? Well, when you change the data stored inside a variable its previous data is discarded by the system and can no longer be accessed.