Get exit code from command substitution

When executing the following line in bash:

set -e
p=$(mktemp -t "${1}.$$.XX")

mktemp not working with this message:

+++ mktemp -t cpfs.c.o.5643.XX
mktemp: too few X in template `cpfs.c.o.5643.XX'

How can I get a failure error to enable errors during command replacement? Alternatively, how can I spread the mktemp return form back so that set -e, or my own code can impact the result?

+3
source share
1 answer

The return code of the last command is always stored in $?.

do something like:

command
ERR=$?

In order not to lose this return code for future use.

+3
source

Source: https://habr.com/ru/post/1759638/


All Articles