Correctly quote config values while saving

If the value contains a command (; or #) char or starts or ends with space.

Fixes issue #1948