Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14830 > unrolled thread

Re: Misbehavior with constants and bash script

Started byChet Ramey <chet.ramey@case.edu>
First post2018-11-19 18:38 -0500
Last post2018-11-19 18:38 -0500
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Misbehavior with constants and bash script Chet Ramey <chet.ramey@case.edu> - 2018-11-19 18:38 -0500

#14830 — Re: Misbehavior with constants and bash script

FromChet Ramey <chet.ramey@case.edu>
Date2018-11-19 18:38 -0500
SubjectRe: Misbehavior with constants and bash script
Message-ID<mailman.4283.1542670717.1284.bug-bash@gnu.org>

[Multipart message — attachments visible in raw view] — view raw

On 11/19/18 3:04 PM, Alexander Reintzsch wrote:
> Hello,
> 
> I think I have found some unexpected behavior related to constants in
> bash scripts. I have attached the short proof of concept bash script.
> 
> Usually bash scripts continue when they face an error with one command
> but this script shows some weired behavior. It exits all the functions
> it has called without executing the remaining commands and the continues
> to run in the top scope of the script.
> 
> This only happens when a constant declared with
> declare -r myConst="myConstantValue"
> is attempted to be redefined using
> myConst="new value"
> but not with
> declare myConst="new value"
> 
> This behavior doesn't seem right.

OK, let's unpack this.

The variable assignment without declare has the posix semantics, in that
it constitutes a variable assignment error. When the shell is in posix
mode, it exits. When not in posix mode, the shell returns to the top level
and continues execution. It could return to the previous execution context
-- a virtual `return' -- or exit as it does in posix mode but has never
done so.

When the assignment is used as an argument to `declare', it causes the
declare command to fail, but it's not a variable assignment error, so
the script simply continues as with any other failed command.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web