'How to Calculate Execution Time and Get Command Exit Code

I write a shell script and I want to get execution time of a command but I also redirect output of command to a file. Also, I need to get exit code of this specific command to use it in if statement. Here is what I have tried:

...
TIMEFORMAT=%R

log_file() {
  echo "$PROJECT_DIR/logs/$1_$(date +%Y%m%d_%H%M%S).log"
}    

CODE_TIME=$(time sh -c "ipython -c "%run ./foo.ipynb" > $(log_file "log_file_name") 2>&1")

CODE_RESULT=$?

echo "Code_Result: $CODE_RESULT"

if [ "$CODE_RESULT" -eq 0 ]

    ...

According to documentation of time, it returns the exit code of the command but I also execute an echo command in function. So, anyone has an idea about how can I get exit code of ipython command? Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source