help-bash
[Top][All Lists]
Advanced

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

command substitution subshell exits when assigning to readonly variable


From: Philippe Cerfon
Subject: command substitution subshell exits when assigning to readonly variable
Date: Wed, 18 Oct 2023 00:38:37 +0200

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.



reply via email to

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