我想知道如何声明一个变量而不向它赋值。据bash的文档,这应该是确定:bash - 声明一个变量而不指定值
声明[-aAfFgilnrtux] [-p] [名称[=值] ...]
声明变量和/或给他们的属性。
“= value”位是可选的,但使用“declare var”没有赋值似乎没有任何作用。
#!/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
基本上我会期望有“toto not defined!”只为先“check_toto_set”,和所有其它的应该找到TOTO正在申报,即便是空的,但输出继电器是:
toto not defined!
toto=something
toto defined, unsetting
toto not defined!
toto not defined!
toto=somevalue
toto defined, unsetting
我使用Ubuntu的
echo $BASH_VERSION
4.3.46(1)-release
庆典46年3月4日所以我误解了一些关于声明的内容,或者我测试了一个变量是否被设置为错误的方式? (我使用的信息来自How to check if a variable is set in Bash?)
顺便说一句,'unset'有效undeclares的变量;它不只是删除价值。 –