init.sh: improve portability and efficiency