Use string substitution and shell redirection that works with dash.

Despite the shebang line, this script is invoked by cabal using sh. The default shell on Ubuntu, dash, does not understand bash-isms like ${p:n:m}.