How to calculate used memory on Mac OS by command line?

You can use the macOS equivalent to unix vmstat, which is vm_stat.

➜ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                              139824.
Pages active:                           1673261.
Pages inactive:                         1073096.
Pages speculative:                         2909.
Pages throttled:                              0.
Pages wired down:                        788478.
Pages purgeable:                         226206.
[...]

To calculate the memory, multiply the number of pages with the page size (4096 bytes) and divide by 1024 to adjust the prefix to your needs.