In Zsh parameter expansion, I have:
"${test_var:-"${HOME}/test"}" 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?
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)):-' '}"} Or:
echo ${test_var:-"$((){<<<${1-' '}} ~/test(N))"} f=(~/test(N)); echo ${test_var:-${f:-' '}} into position of ~/test(N) in echo ${test_var:-~/test(N)}?
test_varis not set or null then expand to${HOME}/testbut if${HOME}/testnot exist then expandtest_varto" ", if${HOME}/testexists then expandtest_varto${HOME}/testas usual.