Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get exit code from a command substitution

Tags:

bash

shell

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?

like image 738
Matt Joiner Avatar asked Aug 02 '26 18:08

Matt Joiner


1 Answers

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

do something like:

command
ERR=$?

To not to lose that return code for later use.

like image 118
polemon Avatar answered Aug 05 '26 09:08

polemon