0

I am relatively new to shell scripts. I am trying to create a script that can initiate grunt.js by launching the script in Finder (Mac). I want to be able to place this script anywhere and for it to just work by running the version of grunt in the folder the script is located.

How can I achieve this? I have looked for a few hours trying to find a solution but due to my lack of knowledge I am struggling.

Does anyone have a snippet that can CD to the directory the script is stored and run a command?

George
  • 180
  • 2
  • 12
  • 1
    You may find [this thread](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) useful. – user000001 Sep 21 '13 at 12:48
  • I did look at this, but I could not work out how to CD to that directory. I tried all the snippets but when I tried 'ls' it still showed me the files in the user directory. – George Sep 21 '13 at 12:57
  • 1
    If you call `cd "$(dirname "${BASH_SOURCE[0]}" )"` then from that point on the current directory will be the one that the script is stored in. – user000001 Sep 21 '13 at 12:59
  • Thanks! Its working now :) I don't really have knowledge with shell scripts and this is what I needed. – George Sep 21 '13 at 13:03
  • Use my solution here: https://github.com/lingtalfi/printScriptDir/blob/master/printScriptDir.sh It handles symlinks, relative paths and absolute paths. Tested on linux and macOsX – ling Oct 02 '15 at 22:12

1 Answers1

0

If you call cd "$(dirname "${BASH_SOURCE[0]}" )" then from that point on the current directory will be the one that the script is stored in.

imp25
  • 2,327
  • 16
  • 23
George
  • 180
  • 2
  • 12