Zend certified PHP/Magento developer

How to correctly export env variables containing spaces in ZSH

Add an env variable containing spaces to ~/.zshrc

export SKIP='-Dskip1 -Dskip2'

or

export SKIP="-Dskip1 -Dskip2"

or

export SKIP=-Dskip1 -Dskip2

Try to use it

set -x  
mvn $SKIP  

The command that is being executed is actually

+-zsh:108> mvn ‘-Dskip1 -Dskip2’

How to avoid the single quotes being added ?

zsh 5.7.1 (x86_64-apple-darwin19.0)

If not using quoting/escaping at all, there is an error when opening the shell:

/.zshrc:export:11: not valid in this context: -Dskip2


To make it more clear, add 2 env variables to ~/.zshrc

export SKIP='-Dskip1 -Dskip2'
export SKIP_NO_SPACES='-Dskip3'

Try them out

set -x
mvn $SKIP $SKIP_NO_SPACES

Here’s the result:

+-zsh:3> mvn ‘-Dskip1 -Dskip2’ -Dskip3

Only the variable that contains spaces gets quoted.