On bash, this assumes that $HISTCONTROL is set to ignoreboth [0]. This is in the default ~/.bashrc on Ubuntu, but I can't speak about other distros.
I don't use zsh, but I believe the equivalent is $HIST_IGNORE_SPACE [1].
[0] https://linux.die.net/man/1/bash
[1] https://unix.stackexchange.com/questions/6094/is-there-any-w...