Bash and declare

kburtch-Zd07PnzKK1IAvxtiuMwx3w at public.gmane.org kburtch-Zd07PnzKK1IAvxtiuMwx3w at public.gmane.org
Wed Mar 29 19:06:50 UTC 2006


Neil Watson <tlug-neil-8agRmHhQ+n2CxnSzwYWP7Q at public.gmane.org> said:

> On Wed, Mar 29, 2006 at 11:03:40AM -0400, kburtch-Zd07PnzKK1IAvxtiuMwx3w at public.gmane.org wrote:
> >In addition to assigning attributes, declare is used with the nounset option.
> > By declaring variables, Bash is able to check for spelling mistakes in
> >variables.  Otherwise Bash assumes that a badly spelled variable name is
> >really a new variable, something Perl doesn't do.
> 
> #!/bin/bash
> 
> declare nounset MYVAR=50
> 
> MIVAR=42
> 
> printf $MYVAR
> 
> How does declare prevent mispelled variables?

It appears my memory is going on me.  Some experimentation shows that declare
has no affect on nounset.

#!/bin/bash
shopt -s -o nounset
# (or set -u)

declare -i TOTAL=0
TOTAL=TTOAL+1

Results in

t.sh: line 5: TTOAL: unbound variable

Without nounset, the script fragment runs with no errors.  But removing
declare has no effect on the error.  nounset gives an error if you reference
an unbound variable, not if you're assigning values to a mispelled name.

So I'll have to concede with Mr. Browne that declare is for creating variables
 such as integers and arrays which would otherwise be a simple string variable.

> BTW, when using the Strict module in Perl the variable MIVAR above could
> not happen.

I guess I always use strict so I never noticed that Perl auto-declares
variables without it.

Ken B

-- 



--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list