Matt Joiner
Matt Joiner

Reputation: 118540

Get exit code from a command substitution

In executing the following line in bash:

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

mktemp fails with this message:

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

How can I have the error on fail include errors during command substitutions? Alternatively, how can I propagate the return code form mktemp back such that set -e, or my own code can act on the result?

Upvotes: 3

Views: 4262

Answers (1)

polemon
polemon

Reputation: 4772

Return code of last command is always kept in $?.

do something like:

command
ERR=$?

To not to lose that return code for later use.

Upvotes: 3

Related Questions