Skip to content

Instantly share code, notes, and snippets.

@namgivu
Last active February 8, 2023 15:55
Show Gist options
  • Save namgivu/b78bbe4bf71ebe4d6f5ffb5424b52bbf to your computer and use it in GitHub Desktop.
Save namgivu/b78bbe4bf71ebe4d6f5ffb5424b52bbf to your computer and use it in GitHub Desktop.
linux shell script - get script home SH folder
if [ -z "$BASH" ]; then bash $0; exit; fi # forced run w/ bash shell ref. https://stackoverflow.com/a/15731952/248616
SH=$(dirname `realpath $BASH_SOURCE`)
###
SH aka SCRIPT_HOME ie the home folder of the running/executed script file - aka containing folder of .sh script file
ref. https://stackoverflow.com/a/11114547/248616
SH=$(dirname `realpath -s "$0"`) # this works for shell :sh ; CAUTION $0 will always store the FIRST executed .sh ie f0.sh call f1.sh, f2.sh, and $0 always pointing to f0.sh --> must use $BASH_SOURCE to have SH point to the current-script
SH=$(cd `dirname $BASH_SOURCE` && pwd) # this only works for shell :bash and NOT working for :sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment