I have a function calling another, with unexpected results:
#!/bin/bash
function doSomething() {
callee
echo $?
echo "It should go here!"
}
function callee() {
cat line.txt | while read ln
do
echo $ln
if [ 1 ] ;then
{ echo "This is callee" &&
return 2; }
fi
done
echo "It should not go here!"
}
doSomething
Here is the result:
aa
This is callee
It should not go here!
0
It should go here!
Why does return act like break here? I want it to exit the callee function, not just break the loop.
This is because you are using a pipe into a
whileloop, which runs in a subshell (in Bash). You are returning from the subshell, not the function. Try this:Kill the cat!