How to effectively abort the execution of a Bash script from a function
What you could do, is register the top level shell for the TERM
signal to exit, and then send a TERM
to the top level shell:
#!/bin/bash
trap "exit 1" TERM
export TOP_PID=$$
function func()
{
echo "Goodbye"
kill -s TERM $TOP_PID
}
echo "Function call will abort"
echo $(func)
echo "This will never be printed"
So, your function sends a TERM
signal back to the top level shell, which is caught and handled using the provided command, in this case, "exit 1"
.
Difference between return and exit in Bash functions
From man bash
on return [n]
;
Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.
... on exit [n]
:
Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.
EDIT:
As per your edit of the question, regarding exit codes, return
has nothing to do with exit codes. Exit codes are intended for applications/scripts, not functions. So in this regard, the only keyword that sets the exit code of the script (the one that can be caught by the calling program using the $?
shell variable) is exit
.
EDIT 2:
My last statement referring exit
is causing some comments. It was made to differentiate return
and exit
for the understanding of the OP, and in fact, at any given point of a program/shell script, exit
is the only way of ending the script with an exit code to the calling process.
Every command executed in the shell produces a local "exit code": it sets the $?
variable to that code, and can be used with if
, &&
and other operators to conditionally execute other commands.
These exit codes (and the value of the $?
variable) are reset by each command execution.
Incidentally, the exit code of the last command executed by the script is used as the exit code of the script itself as seen by the calling process.
Finally, functions, when called, act as shell commands with respect to exit codes. The exit code of the function (within the function) is set by using return
. So when in a function return 0
is run, the function execution terminates, giving an exit code of 0.
Stopping script from nested function
you can add an exit 0
to exit the script with a 0 exit status at the desired point :
function_2() {
printf "hello "
exit 0
...
How to exit a function in bash
Use:
return [n]
From help return
return: return [n]
Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last command executed within the function or script.
Exit Status:
Returns N, or failure if the shell is not executing a function or script.
In a Bash script, how can I exit the entire script if a certain condition occurs?
Try this statement:
exit 1
Replace 1
with appropriate error codes. See also Exit Codes With Special Meanings.
Terminate a bash script when assigning a function call into a variable
You can check the exit status when assigning the variable, and exit the main script if it failed.
input=$(demo) || exit 1
Related Topics
How to Concatenate Svg Files Lengthwise from Linux Command Line
The Most Reliable Way to Terminate a Family of Processes
How to Prevent Linux from Initializing a Usb Hid Device
Google Cloud Storage Buckets: Mounting in a Linux Instance with Global Permissions
Page Fault in Interrupt Context
Why Cannot I Directly Compare 2 Thread Ids Instead of Using Pthread_Equal
How Does Linux Handles The I/O Permission Bitmap in The Tss Structure
Dialog in Bash Is Not Grabbing Variables Correctly
Nohup Failing with Anaconda Ipython
How to Schedule a Cron for The First Thursday of Every Month
Documentation About Device Driver Programming on Kernel 3.X
Avoid Copying of Data Between User and Kernel Space and Vice-Versa
How Connect Variable with a String in Bash
Accidentally Deleted Symlink Libc.So.6 in Centos 6.4. How to Get Sudo Privilege to Re-Create It