In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the local function body only, but pass the original exported value to the child process?

function the_func {
  local MY_VAR="new value"

  echo "=== $MY_VAR"  # === prints "new value"
  my -child -process  # === gets MY_VAR="original"

export MY_VAR="original"

A subshell and readonly variables won't be good enough because the variables are also needed outside of the subshell. The args or name of the command could be in a variable that might class with the child process.


1 Answer 1

  1. mksh
  2. Bash with declare +x MY_VAR=value. (local +x and typeset +x also work).

However, in Bash, it would fail under these conditions:

my_func () {
   local +x MY_VAR="..."
   bash my.sh  # ERROR in Bash: this gets "original" instead of "2nd def"
               # Works in mksh: "2nd def" is passed.
export MY_VAR="original"
MY_VAR="2nd def" my_func

It behaves intuitively in mksh.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .