[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
typeset -p on an empty integer variable is an error. (plus -v test w/ ar
From: |
Dan Douglas |
Subject: |
typeset -p on an empty integer variable is an error. (plus -v test w/ array elements) |
Date: |
Fri, 11 Jan 2013 12:27:44 -0600 |
User-agent: |
KMail/4.8.3 (Linux/3.4.6-pf+; KDE/4.8.3; x86_64; ; ) |
Bash treats the variable as essentially undefined until given at least an
empty value.
$ bash -c 'typeset -i x; [[ -v x ]]; echo "$?, ${x+foo}"; typeset -p x'
1,
bash: line 0: typeset: x: not found
$ ksh -c 'typeset -i x; [[ -v x ]]; echo "$?, ${x+foo}"; typeset -p x'
0,
typeset -i x
Zsh implicitly gives integers a zero value if none are specified and the
variable was previously undefined. Either the ksh or zsh ways are fine IMO.
Also I'll throw this in:
$ arr[1]=test; [[ -v arr[1] ]]; echo $?
1
This now works in ksh to test if an individual element is set, though it
hasn't always. Maybe Bash should do the same? -v is tricky because it adds
some extra nuances to what it means for something to be defined...
--
Dan Douglas
signature.asc
Description: This is a digitally signed message part.
- typeset -p on an empty integer variable is an error. (plus -v test w/ array elements),
Dan Douglas <=
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), John Kearney, 2013/01/11
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), Dan Douglas, 2013/01/11
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), John Kearney, 2013/01/11
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), Dan Douglas, 2013/01/12
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), John Kearney, 2013/01/12
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), Chet Ramey, 2013/01/12
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), John Kearney, 2013/01/12
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), Greg Wooledge, 2013/01/14
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), John Kearney, 2013/01/14
- Re: typeset -p on an empty integer variable is an error. (plus -v test w/ array elements), Greg Wooledge, 2013/01/14