Skip to content

Instantly share code, notes, and snippets.

@cowlicks
Created June 22, 2023 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowlicks/9fa3d8936d3d926ce8e55fdba23322a5 to your computer and use it in GitHub Desktop.
Save cowlicks/9fa3d8936d3d926ce8e55fdba23322a5 to your computer and use it in GitHub Desktop.
Location agnostic bash script. Demonstrate using paths relative to script, and working directory. Return to working directory on script end.
#!/usr/bin/env bash
# exit on first failure
set -e
# uncomment to enable debug mode
#set -x
# The location this script is run from
OG_DIR=$(pwd)
# The directory that this script is in
SCRIPT_DIR=$(dirname -- "$0");
# Return to orignal director the script was run from
trap "cd $OG_DIR" EXIT
# Go somewhere or do something relative script location. The `trap` will put us back later
pushd "$SCRIPT_DIR/.."
echo "HELLO THESE ARE THE ARGS $@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment