Is there something between exit
and return 1
in bash? Some command that would guarantee stop of further processing but not exit terminal?
Meaning if I use exit
in a sourced function in my bash anytime exit
is invoked it will actually quit bash (or log off of ssh connection if I am on remote host). If I use return 1
then I have to check the value in the calling function.
With return I have to write code like the following:
foo(){
if [[ "$#" -ne 1 ]]; then
echo "Unexpected number of arguments [actual=$#][expected=1]"
return 1
fi
# ... do stuff.
}
bar(){
foo
if [[ "$?" -ne 0 ]]; then
echo "Line:$LINENO failure"
return 1;
fi
# do stuff only when foo() is successful
}
I could use an exit but as described then I will quit my current bash if the operation is not succesful:
foo(){
if [[ "$#" -ne 1 ]]; then
echo "Unexpected number of arguments [actual=$#][expected=1]"
exit
fi
# ... do stuff.
}
bar(){
foo
# do stuff only when foo() is successful
}
What I would like is something like:
foo(){
if [[ "$#" -ne 1 ]]; then
echo "Unexpected number of arguments [actual=$#][expected=1]"
# Simulate CTRL+C press (to cause everything to halt but not exit terminal)
# Like an exception throw or something?
fi
# ... do stuff.
}
bar(){
foo
# do stuff only when foo() is successful
}