[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Inconsistent termination of function when 'local' tries to override read
From: |
Arfrever Frehtes Taifersar Arahesis |
Subject: |
Inconsistent termination of function when 'local' tries to override read-only variable |
Date: |
Wed, 19 Feb 2020 20:36:46 +0100 |
When scalar variable is read-only, then calling 'local' for this
variable (regardless of presence of value in assignment) is non-fatal
and subsequent commands in function are executed.
When (indexed or associative) array is read-only, then calling 'local
-a' or 'local -A' (without value) for this variable is non-fatal and
subsequent commands in function are executed.
Trying to set global variable without using 'local' is fatal.
Trying to set global variable using 'local -g' is non-fatal.
In other cases, function is terminated immediately.
(BASH 5.0.16.)
$ declare -r VAR1=a
$ declare -ar VAR2=(a)
$ declare -Ar VAR3=([a]=a)
$ f() { local VAR1 VAR2 VAR3 ; echo AAA ; } ; f
bash: local: VAR1: readonly variable
bash: local: VAR2: readonly variable
bash: local: VAR3: readonly variable
AAA
$ f() { local VAR1=b VAR2=b VAR3=b ; echo AAA ; } ; f
bash: local: VAR1: readonly variable
bash: local: VAR2: readonly variable
bash: local: VAR3: readonly variable
AAA
$ f() { local -a VAR1 VAR2 VAR3 ; echo AAA ; } ; f
bash: local: VAR1: readonly variable
bash: local: VAR2: readonly variable
bash: local: VAR3: readonly variable
AAA
$ f() { local -a VAR1=(b) ; echo AAA ; } ; f
bash: f: VAR1: readonly variable
$ f() { local -a VAR2=(b) ; echo AAA ; } ; f
bash: f: VAR2: readonly variable
$ f() { local -a VAR3=(b) ; echo AAA ; } ; f
bash: f: VAR3: readonly variable
$ f() { local -A VAR1 VAR2 VAR3 ; echo AAA ; } ; f
bash: local: VAR1: readonly variable
bash: local: VAR2: readonly variable
bash: local: VAR3: readonly variable
AAA
$ f() { local -A VAR1=([b]=b) ; echo AAA ; } ; f
bash: f: VAR1: readonly variable
$ f() { local -A VAR2=([b]=b) ; echo AAA ; } ; f
bash: f: VAR2: readonly variable
$ f() { local -A VAR3=([b]=b) ; echo AAA ; } ; f
bash: f: VAR3: readonly variable
$ f() { local -g VAR1= VAR2= VAR3= ; echo AAA ; } ; f
bash: local: VAR1: readonly variable
bash: local: VAR2: readonly variable
bash: local: VAR3: readonly variable
AAA
$ f() { VAR1= ; echo AAA ; } ; f
bash: VAR1: readonly variable
$ f() { VAR2= ; echo AAA ; } ; f
bash: VAR2: readonly variable
$ f() { VAR3= ; echo AAA ; } ; f
bash: VAR3: readonly variable
$
--
Arfrever Frehtes Taifersar Arahesis
- Inconsistent termination of function when 'local' tries to override read-only variable,
Arfrever Frehtes Taifersar Arahesis <=