Skip to content

Instantly share code, notes, and snippets.

@noel-yap
Last active April 23, 2024 15:42
Show Gist options
  • Save noel-yap/814ee650070336246443 to your computer and use it in GitHub Desktop.
Save noel-yap/814ee650070336246443 to your computer and use it in GitHub Desktop.
# Allows one to have a multi-line shebang just like in Scala. Just as in Scala, be sure to end the multi-line shebang with a !#.
# example usage: trampoline "${GRADLE_HOME}/bin/gradle" "$@"
function trampoline() {
local command=("$@")
umask 077
tmpdir="$(mktemp -d -t $$.XXXXXXXXXXXXXXXX)"
trap 'rm -rf -- "${tmpdir}"' INT TERM HUP EXIT
tmpfile="${tmpdir}/$(basename $0)"
command=("${command[@]/$0/${tmpfile}}")
sed -e '1,/^!#$/d' "$0" >"${tmpfile}" && "${command[@]}"
status=${PIPESTATUS[0]}
exit ${status}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment