Move some code in bash in an internal helper