7

I thought typeset was ksh's local, but this fails in ksh93 although it works in all my other typeset-supporting shells (bash, yash, zsh, pdksh)

#!/bin/ksh -ex

foo(){
    typeset a b
    a=0; b=1
    return
}
a=a; b=b
foo
#confirm that the globals didn't change
[ "$a" = a ] 
[ "$b" = b ]

What gives?

1 Answer 1

12

typeset is ksh93's private (using static scoping like perl's my, not local which does dynamic scoping) only for functions that are declared using the ksh function definition style:

function foo {
  typeset var=whatever
  ...
}

With the Bourne syntax (or with the . command (which btw, can also be used on ksh-style functions)), there's no scoping (except for $1, $2... $# of course). So one can use Bourne-style functions to get the value or change the value or type of a variable in the parent context (though typeset -n can also be used for that with the ksh-style.

In ksh88, typeset was doing dynamic scoping with both the ksh and Bourne function definition style. According to David Korn, POSIX did not specify ksh's variable scoping on the basis that it was dynamic (deemed inferior) which is why he changed it to static scoping for ksh93 (a complete rewrite).

But in the mean time, other shells have implemented variable scoping and they all did it using dynamic scoping to mimic ksh88's.

zsh now has a private keyword to have scoping similar to ksh93's in addition to local/typeset with dynamic scoping like in ksh88.

To see the difference between static and dynamic scoping, compare:

"$shell" -c 'function f { typeset a=1; g; echo "$a"; }
             function g { echo "$a"; a=2; }
             a=0; f'

Which with $shell == ksh93 outputs:

0
1

And with ksh88 or bash outputs:

1
2

zsh:

$ zsh -c 'zmodload zsh/param/private
          f() { private a=1; g; echo $a;}
          g() { echo $a; a=2; }
          a=0; f'
0
1

To be able to use local scope in code portable to bash, zsh, ksh88 ksh93, pdksh, yash or dash/FreeBSD sh, you could do:

[ -n "$BASH_VERSION" ] && shopt -s expand_aliases
alias shdef= kshdef='#'
if type typeset > /dev/null 2>&1; then
  alias mylocal=typeset
  if (a=1; f() { typeset a=2; }; f; [ "$a" = 2 ]); then
    alias shdef='#' kshdef='function'
  fi
else
  alias mylocal=local
fi

And then declare your functions as:

kshdef foo
shdef foo()
{
  mylocal var
  var=value
  ...
}

In any case, there are many differences between the behaviour of those local in the various shells. Beside the dynamic vs static consideration mentioned above, there's whether variables initially get an unset or empty value or inherit the value from the parent scope. And there's the interaction with readonly, unset, whether local/typeset is a keyword or builtin (affects split+glob handling)...

There are other implications of using the ksh-style function definition in ksh93, see the man page for details.

More reading

13
  • 1
    Thanks. Do you know if it's possible to get the same effect as local in ksh93 in without the function-based function definitions? The reason I'm asking is I have a bunch of mostly POSIX code except with local variables and so I thought I'd make it more portable by replacing the local declarations with eval $current_shells_local_keyword $the_variables. Commented Jul 4, 2017 at 9:42
  • @PSkocik, you may want to have a look at github.com/modernish/modernish Commented Jul 4, 2017 at 9:43
  • 1
    @PSkocik, see edit for one approach. Commented Jul 4, 2017 at 10:08
  • I like your approach but ksh support wasn't essential, and I ended up ignoring it and simply doing alias local=typeset for yash. With that, my code appears to be correct in dash, bash, yash, posh, and zsh, as long as I don't rely on a specific type of scoping, the local values being auto-initialized to empty (in some shells they aren't), and on local being a keyword ( local a=$b is nonportable but local a="$b" or local a; a=$b appears to work across all the shells). Thanks for the help. Commented Jul 5, 2017 at 15:36
  • Even worse, the version of ksh93 on RHEL6.10 (shown as sh (AT&T Research) 93u+ 2012-08-01) and the version on RHEL7.8 (same version string) have different typeset behaviors, similar to the ksh88/ksh93 changes. For example: echo $HOME; typeset -u HOME; echo $HOME. On RHEL6 this uppercases $HOME. On RHEL7 this uppercases and clears $HOME. Commented Sep 9, 2021 at 15:14

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.