I am trying to write a function in a bash script which accepts a command as an argument, executes the command -- AND -- if the command non-zero exited, abort the script:file: command_wrapper.sh#!/usr/bin/env bashfunction exec_cmd() {    local command=${1}    $command || exit            # run the command if $? is 1 exit    if [[ $? -eq 1 ]]           # AGAIN if $? is 1 exit    then        echo command failed        exit    fi    return 0}exec_cmd grep -R somsomeoemoem .      # call the function, passing a                                         # command that will exit with 1echo rest of script runningcall it:% ./command_wrapper.sh./command_wrapper.sh:exec_cmd grep -R somethingyouwillneverfind .rest of script runningthe script carries on after the function is run with a command that non-zero exits - why?How can I get this command to error exit when $command fails?
bash: How can I detect if the previous command failed, then exit - from inside a function?
bash
Your function works as designed but the issue is your assumption is incorrect. grep -R somsomeoemoem . doesn't exit in error.Try with this command to see the function exiting:exec_cmd false