If you don't mind subverting bash syntax you can create a function to perform the assignment and set the readonly attribute. Here's a worked example.
First create a function called assign that assigns a variable to the output from a command and sets it to be readonly. An assignment error will send a TERM signal to the program itself, causing it to abort.
assign() {
local _n="$1" _ss
shift
local _errTrap="$(trap -p ERR)" _setE=$([ -o errexit ] && echo 'set -e')
trap '
echo "TRAP/ERR: PID=$$ bad assignment to variable: ${_n:-<unknown>}" >&2
kill "$$"
' ERR
set +e
eval "$_n"'=$("$@")'
_ss=$?
readonly "${_n}"
if [ -n "$_errTrap" ]; then eval "$_errTrap"; else trap - ERR; fi
if [ -n "$_setE" ]; then set -e; fi
return "$_ss"
}
Now let's use it:
echo 'Assign x=$(date)'
assign x date
echo "x=$x"
And:
echo 'Assign y=$(false)'
assign y false
echo "y=$y"
And finally:
echo 'Fail to assign to readonly x'
x=123
You can use this approach in a function by localising a variable and then assigning it:
f() {
local d; assign d date
echo "f(): d=$d" >&2
…
}
set -eis not recommended in general. Unless you're using that, I think you can just ignore that shellcheck warning.