The difference between exit and return in a function
From:
Ronald van Gogh
Subject:
The difference between exit and return in a function
Date:
Thu, 28 Feb 2002 22:47:47 +0100
According to my documentation a return statement in a function under bash
should end the function and have the calling part of the script continue.
With an exit statement you should be able to leave the entire script.
However looking at the script and the output below there seems to be no
difference between return and exit.
Has anybody an idea how i can end the entire script from within a function ?
Script:
-------
#!/usr/local/bin/bash
function mytest () {
local param=$1
case $param in
1) echo "Option 1"
return 0
;;
2) echo "Option 2"
return 1
;;
3) echo "Option 3"
exit 2
;;
*) echo "Invalid option"
return 9
;;
esac
return 5
}
aa=$(mytest 1); echo "rc = $?; result = $aa"
aa=$(mytest 2); echo "rc = $?; result = $aa"
aa=$(mytest 3); echo "rc = $?; result = $aa"
aa=$(mytest 4); echo "rc = $?; result = $aa"
Results:
--------
rc = 0; result = Option 1
rc = 1; result = Option 2
rc = 2; result = Option 3
rc = 9; result = Invalid option
I would have expected that the last 2 lines would not have appeared.
Kind regards, Ronald van Gogh
_________________________________________________________________