help-bash
[Top][All Lists]
Advanced

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

Re: command substitution subshell exits when assigning to readonly varia


From: alex xmb sw ratchev
Subject: Re: command substitution subshell exits when assigning to readonly variable
Date: Wed, 18 Oct 2023 17:22:49 +0200

On Wed, Oct 18, 2023, 17:20 alex xmb sw ratchev <fxmbsw7@gmail.com> wrote:

>
>
> On Wed, Oct 18, 2023, 00:39 Philippe Cerfon <philcerf@gmail.com> wrote:
>
>> Dear list.
>>
>> When I have a script like:
>> echo 1
>> V="$(
>> echo a >&2
>> false
>> echo b >&2
>> )"
>> echo 2
>>
>> I get:
>> 1
>> a
>> b
>> 2
>> when executing or sourcing it.
>>
>>
>> But when I have:
>> readonly R=.
>>
>> echo 1
>> V="$(
>> echo a >&2
>> R=
>> echo b >&2
>> )"
>> echo 2
>>
>> I get:
>> 1
>> a
>> b.sh: line 9: R: readonly variable
>> 2
>> when executing it, and something similar when sourcing it.
>>
>>
>> set -e is not used. And something like R= || true doesn't help either.
>>
>>
>> This does not seem to happen, when outside a command substitution:
>> readonly R=.
>>
>> echo 1
>> R=
>> echo 2
>>
>> gives:
>> 1
>> bash: R: readonly variable
>> 2
>> regardless of whether executed or sourced.
>>
>>
>> Also:
>> R= 2> /dev/null
>> doesn't do what one might hope it would. I assume this is because the
>> redirection happens after the assignment?
>> So the only thing one could do is exec 2> /dev/null before?
>>
>
> another way follows
>

oh uhm sorry , to tell in short .. it looks for ending ' r ' for readonly
in declare -p _var_ output

( a=1 ; readonly b=2 c=( 3 ) ; declare -p a b c ; for t in a b c ; do v=(
> $( declare -p "$t" ) ) p=${v[1]: -1} ; if [[ $p == r ]] ; then printf 'plz
> dont try to set already readonly var\n' ; else printf 'go ahead and
> %s=_wanted_content\n' "$t" ; fi ; done )
>
> declare -- a="1"
> declare -r b="2"
> declare -ar c=([0]="3")
> go ahead and a=_wanted_content
> plz dont try to set already readonly var
> plz dont try to set already readonly var
>
> Regards,
>> Philippe.
>>
>>


reply via email to

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