rewritten bash (non portable) code into standard posix

tested on dash 057