> On 25 Oct 2016, at 05:40, Martijn Dekker <mart...@inlv.org> wrote: > > Op 25-10-16 om 00:42 schreef Stuart Shelton: >> Failing this, is there any alternative to ‘typeset’ to list a >> variable declared as local to a function but which has not yet been >> assigned a value? > > Try simply testing the exit status of 'typeset -p' or 'declare -p'. If > the variable is not declared, it exits unsuccessfully. > > if typeset -p "$var" >/dev/null 2>&1 && [[ ! -v $var ]] > then ... > > As far as I can tell, this is not documented in 'help' or in the info > page, by the way. Perhaps it should be. > > Also note that, while zsh and yash share this behaviour, ksh93 and > mksh/pdksh do not. If you want to be compatible with all the shells that > support 'typeset', you could do: > > if [ -n "$(typeset -p "$var" 2>/dev/null)" ] && > eval "[ -z \"\${var+s}\" ]" > then ... > > which is slower because it uses a subshell. > > HTH, > > - M. >
Hi Martijn, Thanks for the suggestion! However, it doesn’t appear to be able to detect local variables (is this intentional or a bug?): $ test() { local testvar echo “typeset test:" typeset -p testvar && echo yes || echo no echo “grep test:" typeset -p | grep testvar } $ test typeset test: -bash: typeset: testvar: not found no grep test: declare -- testvar yes Cheers, Stuart