I'm trying to figure out how to declare a variable without assigning a value to it. According to the bash doc, that should be ok:
declare [-aAfFgilnrtux] [-p] [name[=value] ...]
Declare variables and/or give them attributes.
The "=value" bit is optional, but using "declare var" without an assignment doesn't seem to do anything.
#!/bin/bash
function check_toto_set() {
if [ -z "${toto+x}" ] ; then
echo toto not defined!
else
echo toto=$toto
echo toto defined, unsetting
unset toto
fi
}
function set_toto() {
declare -g toto
}
function set_toto_with_value() {
declare -g toto=somevalue
}
check_toto_set
toto=something
check_toto_set
declare toto
check_toto_set
set_toto
check_toto_set
set_toto_with_value
check_toto_set
Basically I would expect to have "toto not defined!" just for the first "check_toto_set", and all the others should find toto being declared, even if empty but the ouput is:
toto not defined!
toto=something
toto defined, unsetting
toto not defined!
toto not defined!
toto=somevalue
toto defined, unsetting
I'm using bash 4.3.46 on ubuntu
echo $BASH_VERSION
4.3.46(1)-release
So am I misunderstanding something about declare, or am I testing if a variable is set the wrong way? (I'm using info from How to check if a variable is set in Bash? for that)
unseteffectively undeclares a variable; it doesn't just remove the value.