Skip to content

Instantly share code, notes, and snippets.

@LozanoMatheus
Last active August 26, 2019 15:02
Show Gist options
  • Save LozanoMatheus/da96b4e44b89b13ad4af10ac4602ad99 to your computer and use it in GitHub Desktop.
Save LozanoMatheus/da96b4e44b89b13ad4af10ac4602ad99 to your computer and use it in GitHub Desktop.
Bash/Shell - Getting the script paths (full and relative paths)
#!/usr/bin/env bash
RELATIVE_PATH="${0}"
RELATIVE_DIR_PATH="$(dirname "${0}")"
FULL_DIR_PATH="$(realpath "${0}" | xargs dirname)"
FULL_PATH="$(realpath "${0}")"
echo "RELATIVE_PATH->${RELATIVE_PATH}<-"
echo "RELATIVE_DIR_PATH->${RELATIVE_DIR_PATH}<-"
echo "FULL_DIR_PATH->${FULL_DIR_PATH}<-"
echo "FULL_PATH->${FULL_PATH}<-"
# ## Output
# RELATIVE_PATH->./bin/startup.sh<-
# RELATIVE_DIR_PATH->./bin<-
# FULL_DIR_PATH->/opt/my_app/bin<-
# FULL_PATH->/opt/my_app/bin/startup.sh<-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment