[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: command substitution subshell exits when assigning to readonly varia
From: |
ikhxcsz7y xmbott |
Subject: |
Re: command substitution subshell exits when assigning to readonly variable |
Date: |
Wed, 18 Oct 2023 00:59:23 +0200 |
~ $ readonly r=.
~ $ { r= ; } 2>/dev/null
~ $ { r= ; } #2>/dev/null
bash: r: readonly variable
~ $
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?
>
> Regards,
> Philippe.
>
>