In Zsh parameter expansion, I have:


but here I want to check if ${HOME}/test is exist or not, if not exist then test_var expand to " " (one space string).

Is there an inline solution for this?

  • And if it exists, what happens ? – don_crissti Nov 27 '18 at 20:22
  • @don_crissti: If test_var is not set or null then expand to ${HOME}/test but if ${HOME}/test not exist then expand test_var to " ", if ${HOME}/test exists then expand test_var to ${HOME}/test as usual. – Tuyen Pham Nov 28 '18 at 2:24
echo ${test_var:-~/test(N)}

comes close. By adding a (N) (and here as we are in list context), that ~/test(N) becomes a glob, and because of that N glob qualifier, if there's no test directory entry in $HOME (~), then the glob expands to nothing.

That's different from what you're asking in that it expands to nothing at all, instead of a one space argument.

For that, you could still do it in to steps:

f=(~/test(N)); echo ${test_var:-${f:-' '}}

You could also use command substitution, though that's a bit cheating:

echo ${test_var:-"${$(printf %s ~/test(N)):-' '}"}


echo ${test_var:-"$((){<<<${1-' '}} ~/test(N))"}
  • How to put f=(~/test(N)); echo ${test_var:-${f:-' '}} into position of ~/test(N) in echo ${test_var:-~/test(N)}? – Tuyen Pham Dec 2 '18 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.