1

When I want to take action on a failure I do:

if ! commmand; 
then
    echo $?
fi

But the exit code is always 0.

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

2 Answers2

2

If you need to exit status later, you might consider saving it in a variable:

command
status=$?
if (( status != 0 )); then
    echo $status
fi
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

I fixed it by removing the ! negation and using an empty then block:

if commmand; 
then :
else
    echo $?
fi

Related questions:

  1. Bash conditional based on exit code of command
  2. Exit Shell Script Based on Process Exit Code
  3. How to use bash return code in conditional?
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144