Skip to main content
edited body
Source Link
user232326
user232326

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 $INPUT$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.

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 $INPUT (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.

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.

Source Link
user232326
user232326

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 $INPUT (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.