Simplest solution:
{
sleep 10s
... your echocode "test"...
exit
}
The code block {} and the exit directive is what matters. This way, bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.
If you do not want to "execute" the script, but rather to "source" it, you need a different solution. This should work then:
{
sleep 10s
... your echocode "test"...
ret="$?";return "$ret"return 2>/dev/null || exit "$ret"
}
Or thisif you want direct control over exit code:
# presuming bash is run with -e (errexit)
{
sleep 10s
... your echocode "test"...
returnret="$?";return "$ret" 2>/dev/null || exit 0"$ret"
}
Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.