I use bash enough and fumble my way through it that I have little nuggets like this but I never retain them in my brain. So, in contributing to the global bash community and my external brain storage, here is how to get the path that your currently running script lives in.
PATH=$(cd $(dirname $0) ; pwd -P)And since people typically post scripts like this, even one liners, and don't explain them, I'll explain each piece. 1. The first argument is the full file name to the script: $0 2. Get the directory of the script. You'd think we'd stop there, but we keep going: $(dirname $0) 3. Change directories to the directory containing the script: cd $(dirname $0) 4. Run another bash command: ; 5. Get the current working directory, and -P resolves symlinks: pwd -P 6. Assign the directory containing the script and that is not a symlink so the real path to the PATH variable: PATH=$(cd $(dirname $0) ; pwd -P) There you have it!
No comments:
Post a Comment