2

I've created a stack interpreted script using Turtle and I want to be able to get the directory where the script is (regardless of where it's been called from).

This can be done in Bash by parsing $0 or $BASH_SOURCE[0] (as described here, however, I can't work out how to do it with Stack/Turtle?

I've tried the FindBin library but it didn't work (it just returned the current working directory).

My script is currently defined like so:

#!/usr/bin/env stack
{- stack --resolver lts-13.3 script
    --package turtle
    --package text
-}

Is this possible with a stack script?

tomphp
  • 307
  • 1
  • 4
  • 10

1 Answers1

2

I think you can use the executable-path package to get this information:

https://www.stackage.org/haddock/nightly-2019-07-07/executable-path-0.0.3.1/System-Environment-Executable.html

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
  • 1
    The `getScriptPath` function from that package worked perfectly; the docs say it's experimental but it did exactly what I was hoping for. Thank you for your reply! – tomphp Jul 07 '19 at 19:24