[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] admit 'typeset' is here to stay

From: Dan Douglas
Subject: Re: [PATCH] admit 'typeset' is here to stay
Date: Sun, 21 Feb 2016 00:07:16 -0600

On Sat, Feb 20, 2016 at 5:28 PM, Martijn Dekker <address@hidden> wrote:
> Am I missing something? I thought they did exactly the same thing in
> bash. If I'm not wrong about that, then as far as bash is concerned,
> they are in fact synonyms and functionally equivalent.

Yes declare and typeset are identical in bash.

> 'typeset' and 'declare' in bash are both dynamically scoped, aren't
> they? Where is the difference between them?

typeset has more historical baggage. If you're sticking to bash then use
whichever you prefer. though the only reason you'd typically use typeset
is if you're not planning on doing that, in which case you have to care
about the differences.

> 'typeset' in AT&T ksh93 is statically scoped, but ksh93 is the only
> shell to have statically scoped local variables -- all the other shells
> with 'typeset' (pdksh/mksh, bash, zsh, yash) have it as in ksh88,
> dynamically scoped -- and it doesn't look like this is ever going to
> change, or it would have changed sometime in the 23 years since ksh93
> came out.

It may not change, but dynamic scope can be a handicap in all of
those. Regardless, "local" is reasonably portable to bash and zsh. It's a
default alias to typeset in mksh. Ksh93 v- includes a dynamically-scoped
local (the details of which are somewhat unclear but hopefully that
situation will improve). That leaves old shells like ksh88 that had
dynamically scoped typeset without local. If ksh88 and portability are
that important then you probably don't want to use typeset at all (or
alias it to local and be careful with how it's used, or use typeset if
you know what you're doing).

Scope isn't the only problem. typeset is tied to all of the nonstandard
shell data types. zsh's contiguous integer array type for example that
clobbered `typeset -a` by giving it the same name as ksh/bash sparse
arrays. Even the "emulate" modes don't fix that blunder. Problems like
that are probably a hopeless lost cause. typeset didn't come from bash,
and at least some aspects of it differ wildly between every shell that
has it. (I haven't even gotten into the interaction with "function"
vs "name()" and all the weird scope issues with traps and set options
in {m,}ksh)

>> For the most common requirement: localizing or applying an attribute to
>> a variable within a single function; "local" and the posix-compatible
>> declaration builtins are usually what people want. "local" is supported
>> by every current implementation that also has "typeset" and "declare",
> That would be bash and zsh only, so that's not very meaningful.
> Perhaps you meant "every current implementation that also has
> 'typeset'", which would be ksh93, pdksh/mksh, bash, zsh, and yash.


> The latter two have dynamically scoped 'typeset', so on those, you can
> simply alias 'local' to 'typeset' to get the same functionality with the
> familiar keyword.


> But on ksh93, getting the equivalent of a dynamically scoped 'local' is
> impossible. It has statically scoped local variables only. See Part III,
> Q28 at http://kornshell.com/doc/faq.html

It's not impossible, there are many workarounds even without the bash
compatibility mode. I have lots of scripts that run in both ksh93 and
bash and make heavy use of indirection and locals. Only complex scripts
are very sensitive to the scope issues to begin with.

Bash and mksh are much better nowadays since all three have namerefs
with compatible syntax. The only big missing feature is references
on positional parameters. For simple scripts that's not necessarily
a problem.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]