How to change the number of workspaces from the command line?

You can use dconf:

To get the current values:

dconf read /org/compiz/profiles/unity/plugins/core/hsize
dconf read /org/compiz/profiles/unity/plugins/core/vsize

To set new values:

dconf write /org/compiz/profiles/unity/plugins/core/hsize 2

The schema is org.compiz.core and it is a relocatable scheme (this makes it possible to support multiple profiles for Compiz).

For relocatable schemes you have to add the path if you use gsettings. It's /org/compiz/profiles/unity/plugins/core/ for the Compiz profile unity:

gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize 4
gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ vsize 4