Skip to main content
Became Hot Network Question
minor rephrasing
Source Link

I've read what's listed in Bibliography regarding unset, declare, local and "Shell Functions".

My version of Bash is

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
var='outer'
declare -p var
unset var
declare -p var
function foo {
  echo \""I'm in"\"
  local var='inner'
  declare -p var
  unset var
  declare -p var
}
foo

(the strange quoting around I'm in is there just to preserve syntax highlithing in the following block quote.) This prints

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
declare -- var

There is a difference in unsetting a global variable and unsetting a local variable inside a function. In the former case, the variable is removed. In the latter, the variable stays declared (but unset).

Is there a way to completely remove a local variable inside a function (before the function returns)? That is that the output would be

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
bash: declare: var: not found

That would be useful to test if a variable is non existing inside a function, like in

function foo {
  local var
  while
    declare -p var
  do
    echo "$var"
    ((var++))
    [[ "$var" -eq 4 ]] \
    && unset var
  done
}

This code loops indefinitely, printing


declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var

declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var
[omissis]

Is there anything wrong in wanting to checkchecking if a variable exists using declare -p?

The only mention I found in the Bash reference manual about the difference between unsetting a global variable and a local one is

If a variable at the current local scope is unset, it will remain so (appearing as unset) until it is reset in that scope or until the function returns. (ref. Bash reference manual - sec. "Shell Functions")

where the word appearing is the only clue.

Bibliography

I've read what's listed in Bibliography regarding unset, declare, local and "Shell Functions".

My version of Bash is

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
var='outer'
declare -p var
unset var
declare -p var
function foo {
  echo \""I'm in"\"
  local var='inner'
  declare -p var
  unset var
  declare -p var
}
foo

(the strange quoting around I'm in is there just to preserve syntax highlithing in the following block quote.) This prints

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
declare -- var

There is a difference in unsetting a global variable and unsetting a local variable inside a function. In the former case, the variable is removed. In the latter, the variable stays declared (but unset).

Is there a way to completely remove a local variable inside a function (before the function returns)? That is that the output would be

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
bash: declare: var: not found

That would be useful to test if a variable is non existing inside a function, like in

function foo {
  local var
  while
    declare -p var
  do
    echo "$var"
    ((var++))
    [[ "$var" -eq 4 ]] \
    && unset var
  done
}

This code loops indefinitely, printing


declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var

declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var
[omissis]

Is there anything wrong in wanting to check if a variable exists using declare -p?

The only mention I found in the Bash reference manual about the difference between unsetting a global variable and a local one is

If a variable at the current local scope is unset, it will remain so (appearing as unset) until it is reset in that scope or until the function returns. (ref. Bash reference manual - sec. "Shell Functions")

where the word appearing is the only clue.

Bibliography

I've read what's listed in Bibliography regarding unset, declare, local and "Shell Functions".

My version of Bash is

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
var='outer'
declare -p var
unset var
declare -p var
function foo {
  echo \""I'm in"\"
  local var='inner'
  declare -p var
  unset var
  declare -p var
}
foo

(the strange quoting around I'm in is there just to preserve syntax highlithing in the following block quote.) This prints

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
declare -- var

There is a difference in unsetting a global variable and unsetting a local variable inside a function. In the former case, the variable is removed. In the latter, the variable stays declared (but unset).

Is there a way to completely remove a local variable inside a function (before the function returns)? That is that the output would be

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
bash: declare: var: not found

That would be useful to test if a variable is non existing inside a function, like in

function foo {
  local var
  while
    declare -p var
  do
    echo "$var"
    ((var++))
    [[ "$var" -eq 4 ]] \
    && unset var
  done
}

This code loops indefinitely, printing


declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var

declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var
[omissis]

Is there anything wrong in checking if a variable exists using declare -p?

The only mention I found in the Bash reference manual about the difference between unsetting a global variable and a local one is

If a variable at the current local scope is unset, it will remain so (appearing as unset) until it is reset in that scope or until the function returns. (ref. Bash reference manual - sec. "Shell Functions")

where the word appearing is the only clue.

Bibliography

Source Link

bash - how to remove a local variable (inside a function)

I've read what's listed in Bibliography regarding unset, declare, local and "Shell Functions".

My version of Bash is

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
var='outer'
declare -p var
unset var
declare -p var
function foo {
  echo \""I'm in"\"
  local var='inner'
  declare -p var
  unset var
  declare -p var
}
foo

(the strange quoting around I'm in is there just to preserve syntax highlithing in the following block quote.) This prints

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
declare -- var

There is a difference in unsetting a global variable and unsetting a local variable inside a function. In the former case, the variable is removed. In the latter, the variable stays declared (but unset).

Is there a way to completely remove a local variable inside a function (before the function returns)? That is that the output would be

declare -- var="outer"
bash: declare: var: not found
"I'm in"
declare -- var="inner"
bash: declare: var: not found

That would be useful to test if a variable is non existing inside a function, like in

function foo {
  local var
  while
    declare -p var
  do
    echo "$var"
    ((var++))
    [[ "$var" -eq 4 ]] \
    && unset var
  done
}

This code loops indefinitely, printing


declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var

declare -- var="1"
1
declare -- var="2"
2
declare -- var="3"
3
declare -- var
[omissis]

Is there anything wrong in wanting to check if a variable exists using declare -p?

The only mention I found in the Bash reference manual about the difference between unsetting a global variable and a local one is

If a variable at the current local scope is unset, it will remain so (appearing as unset) until it is reset in that scope or until the function returns. (ref. Bash reference manual - sec. "Shell Functions")

where the word appearing is the only clue.

Bibliography