Skip to main content
2 of 2
edited body
user avatar
user avatar

You must separate the error on the right hand side of an assignement form the command used. If you want a "one line solution" for the declare command, you can use:

value=$(false); declare -r var="$value"

If you do not want to use a new var, you can use builtin ephemeral vars like either $REPLY (used by read) or $_ (the last argument of a previous command line).

Using $REPLY might be as simple as:

REPLY=$(false); declare -r var="$REPLY"

Using $_ may become complex or convoluted.

For me: Using set -e has proven to be more of a problem than a solution. Avoid it.

user232326