Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Last active August 29, 2015 14:19
Show Gist options
  • Save bcalmac/5c6a265707e26be46cd7 to your computer and use it in GitHub Desktop.
Save bcalmac/5c6a265707e26be46cd7 to your computer and use it in GitHub Desktop.
Bash utility function to run a command in a given directory
#!/usr/bin/env bash
# Runs a command in a given directory and preserves the $? of the command
# Example: "runin /src/project mvn install"
function runin {
pushd $1
shift
"$@"
local STATUS=$?
popd
return $STATUS
}
runin . ls runin.sh || echo "Unexpected failure"
runin . ls missing.sh && echo "Unexpected success"
function return-test {
runin . ls missing.sh && return
}
return-test && echo "Unexpected success"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment