nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
78.88k stars 7.9k forks source link

`ksh` doesn't support `local` #574

Open ljharb opened 9 years ago

ljharb commented 9 years ago

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

mklement0 commented 9 years ago

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?

ljharb commented 9 years ago

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.

mklement0 commented 9 years ago

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.

ljharb commented 9 years ago

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

mklement0 commented 9 years ago

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.

jlvivero commented 5 years ago

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

ljharb commented 5 years ago

@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.

jlvivero commented 5 years ago

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

ljharb commented 5 years ago

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".

merlindiavova commented 1 year ago

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?

ljharb commented 1 year ago

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

merlindiavova commented 1 year ago

Will make start today