1

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?

2
  • And if it exists, what happens ? Commented Nov 27, 2018 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. Commented Nov 28, 2018 at 2:24

1 Answer 1

2
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))"} 
1
  • How to put f=(~/test(N)); echo ${test_var:-${f:-' '}} into position of ~/test(N) in echo ${test_var:-~/test(N)}? Commented Dec 2, 2018 at 16:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.