Variables
The command piping feature helps in using the listings of a previous command's result in the next command. But sometimes, you need to use the result of a command that has been executed two or more commands before.
In these kinds of scenarios, you need to store a command's result temporarily in the command chain context and use it later in the command chain. Variables are used to solve these kinds of problems. Types of variables include:
- Local variable - Used whenever a variable is to be accessed only inside the current command chain.
- Global variable - Used whenever a variable is required outside the current command, that is, till the end of the application.
Creating a local variable
set <variable_name> <value>
Creating a local variable
Copiedzmail:>set val 50The above variable can only be accessed in that particular command chain, i.e, the scope of the variable val ends as soon as the current set command ends.
Creating a global variable
To create a global variable which will persist till the application end, include the --global option like below:
Create a global variable
Copiedzmail:>set --global val 50Here, the set command can be accessed till the end of the application.
Getting a variable value
To print the variable value, follow the below command:
get --global val
Get a global variable
Copiedzmail:>get --global val
50The value set to the variable "val" will be displayed.
Note:
When you try to get a local variable, the display will print "null". This is because the local variable lives only till that current command chain, post which it will be removed.
To use the variable values in the commands, use the reference operator as below:
Reference operator command
Copiedzmail:>set accountname johnm | folders list --account $accountname You can refer to both local and global variables in your command. If the variable name you have used is present in both global and local, the local variable will be given higher priority.
For example:
If you need to list mails of an account in a particular folder,
- You need to get the account with the account list command.
- Then use that account and get the folder you need with the folders list command.
- Next, use both the account and folder command to get the message list.
Getting the account
To get the account list based on the Display Name, follow the below command:
Getting the account
Copiedzmail:>account list | filter "Display Name" == johnmOutput
Copied+---------------------+--------------+--------------------------+
| Account Id | Display Name | Email Address |
+---------------------+--------------+--------------------------+
| 8*****************2 | johnm | johnm@zylkertraining.com |
+---------------------+--------------+--------------------------+
Getting the folders
To get a specific list of folders from a specific account, follow the below command:
account list | filter "Display Name" == johnm | folders list --account "$Account Id" | filter "Folder Name" == Spam
Command
Copiedzmail:>account list | filter "Display Name" == johnm | folders list --account "$Account Id" | filter "Folder Name" == SpamOutput
Copied+---------------------+-------------+-------+---------------------+
| Folder Id | Folder Name | Path | Previous Folder |
+---------------------+-------------+-------+---------------------+
| 8*****************4 | Spam | /Spam | 8*****************2 |
+---------------------+-------------+-------+---------------------+Getting the messages
Let us try to run the below command.
account list | filter "Display Name" == johnm | folders list --account "$Account Id" | filter "Folder Name" == Spam | message list --account "$Account Id" --folder "$Folder Id"
This command will fail with the message "Account not found with the given name". This is because when the folders list command is executed, its results will override the accounts list command's results. When the tool tries to search for the Account Id column, there would be no data and hence the error.
To overcome this situation, we use variables.
account list | filter "Display Name" == johnm | set accn "$Account Id" | folders list --account $accn | filter "Folder Name" == Spam | message list --account $accn --folder "$Folder Id"
In the above command, you have used the Set command to set the Account Id value to the variable accn. Then, this variable can be used in the command chain context wherever the Account Id is required.
Command
Copiedzmail:>account list | filter "Display Name" == johnm | set accn "$Account Id" | folders list --account $accn | filter "Folder Name" == Spam | message list --account $accn --folder "$Folder Id"Output
Copied+---------------------+-----------------------------------------+-----------------------+----------------------------------+
| Message Id | Subject | From Address | To Address |
+---------------------+-----------------------------------------+-----------------------+----------------------------------+
| 17***************** | What you’ll learn at ****** 2026! | z*******@*******.com | <johnm@zylkertraining.com> |
+---------------------+-----------------------------------------+-----------------------+----------------------------------+
| 17***************** | CRM Updates: January Edition | news@zohocrm.com | <johnm@zylkertraining.com> |
+---------------------+-----------------------------------------+-----------------------+----------------------------------+As displayed above, the command filters the account with Id first, then moves to the Spam folder to list all the messages in that specific folder.