bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

declare -g var should return a nonzero return code when it fails


From: Peggy Russell
Subject: declare -g var should return a nonzero return code when it fails
Date: Thu, 31 Oct 2013 23:37:43 -0500
User-agent: KMail/1.12.4 (Linux/2.6.31.12-0.2-desktop; KDE/4.3.5; x86_64; ; )

Hi,

"declare -g var" should return a nonzero return code when it fails.

Declaring a local variable as global in a function, has a exit status of 0, 
but the operation was not successful (see test_error).  "help declare" and
the bash man page indicate the exit status will indicate success or failure. 

The "declare -g var", did not just fail with a return code of 0, but it
also removed the variable from the environment. Isn't that grounds for
a nonzero return code... 

# Test Example
#
printf -- '%s ' "address@hidden" ; printf -- '\n\n'

function test_success() {
  declare -g var  ; echo "${FUNCNAME}: rc [$?]"
  var=data1
  declare -p var
}

function test_error() {
  # Locally declared variable
  declare var="data2" ; echo "${FUNCNAME}: rc [$?]"
  declare -p var
  # Redeclare local variable as global
  # Why return code 0?
  # It apparently is not global. Nonexistent.
  declare -g var  ; echo "${FUNCNAME}: rc [$?]"
  declare -p var
  #
  var=data3
  declare -p var
}

test_success
echo "== test_success: [$var]"

echo; unset var
declare -p var
echo

test_error
echo "== test_error: [$var]"

Output:
===============================================
4 2 42 1 release x86_64-suse-linux-gnu

1 test_success: rc [0]
declare -- var="data1"
== test_success: [data1]

./zt: line 28: declare: var: not found

2 test_error: rc [0]
declare -- var="data2"
3 test_error: rc [0]                       <===
./zt: line 18: declare: var: not found     <===
declare -- var="data3"
== test_error: []
===============================================

Peggy Russell



reply via email to

[Prev in Thread] Current Thread [Next in Thread]