nvm icon indicating copy to clipboard operation
nvm copied to clipboard

`ksh` doesn't support `local`

Open ljharb opened this issue 10 years ago • 12 comments

ksh doesn't support local with portable function syntax, and dash doesn't support typeset.

ljharb avatar Nov 06 '14 05:11 ljharb

zsh does have the typeset builtin, and it seems to work as expected (zsh 5.0.5 (x86_64-apple-darwin14.0)):

 zsh -c 'foo() { typeset x=1; echo $x; }; foo; echo ${x-no global x}'

Is there a particular aspect that's not working?

mklement0 avatar Nov 06 '14 13:11 mklement0

Hm, I typed that from memory. There was a reason we can't use typeset - maybe dash doesn't have it? I'll have to investigate.

ljharb avatar Nov 06 '14 18:11 ljharb

Good point: dash has local (which is non-POSIX), but not typeset. Conversely, ksh has typeset, but not local.

An added ksh gotcha is that typeset only creates local variables when using the NON-POSIX function foo { … } syntax.

mklement0 avatar Nov 06 '14 19:11 mklement0

Good to know, thanks! I've updated the issue description.

ljharb avatar Nov 06 '14 19:11 ljharb

If it weren't for the variable-scope issue, you could make ksh happy with this trick:

[ -n "$KSH_VERSION" ] && alias local='typeset' 

foo() {
  local x=2
  echo "$x"
}

foo

echo ${x-no global x}

But, as you'll see, $x became global, because I've used the POSIX function-declaration style (foo() { ... }). Using function foo {...} syntax would result in local variables, wouldn't work in dash (but does work in zsh and bash).

Sadly, there appears to be no configuration item that tells ksh to create local variables with typeset - it appears to be solely controlled by the function-declaration style.

mklement0 avatar Nov 06 '14 19:11 mklement0

So is support for ksh ever going to be a thing?

jlvivero avatar Jan 09 '19 02:01 jlvivero

@jlvivero nvm already should work on ksh; it just creates a lot of global variables due to lack of local.

What I'm looking for is some ergonomic, hard-to-mess-up way to detect and support both local and typeset as needed.

ljharb avatar Jan 09 '19 05:01 ljharb

it's just that when I install nvm with ksh(no option of changing this). I get a LOT of local not found messages. And maybe I'm doing something wrong, but basically if I add export NVM_DIR .... lines to .kshrc whenever I do nvm ls-remote I get more of those messages and /bin/sort as a result

That's why I thought this issue was about support for ksh

jlvivero avatar Jan 09 '19 14:01 jlvivero

Specifically the issue is about how to avoid those warnings :-) nvm should work fine, you'll just be swamped with the warnings, which isn't what i'd call "support".

ljharb avatar Jan 09 '19 20:01 ljharb

In order to get portable local variables, they need to be unset before returning or existing from a function. It should not take too long todo, I'm happy to help if the need is still there?

merlindiavova avatar Mar 29 '23 15:03 merlindiavova

@merlindiavova yes, that would be amazing - please uncomment ksh tests in that PR as well :-)

ljharb avatar Mar 29 '23 16:03 ljharb

Will make start today

merlindiavova avatar Mar 30 '23 12:03 merlindiavova