Question

Let's say I have a Dart script called dart-test. I would like to distribute this script and make it so that users just have to put it in a folder in their $PATH, and execute it from anywhere just by typing dart-test in their terminal.

For the sake of this question, let's pretend I am the user test on my machine. I am on Mac OS X and installed the Dart binary with Homebrew. The dart binary lies in /home/test/.brew/bin and is in the $PATH.

Consequently, the following works:

$ cat <<HEREDOC > ~/.brew/bin/dart-test
#!/home/test/.brew/bin/dart
main() => print('Dart shebang works!');
HEREDOC

$ chmod u+x ~/.brew/bin/dart-test

$ dart-test
Dart shebang works!

The problem is that the Dart shebang I use is not portable, my script won't work on any other computer than mine. Is there a portable way to do this?

Was it helpful?

Solution

(Considering Dart is kind of like Python and Ruby in the way it executes, I just looked at the standard way of doing it in those two languages. The env binary.)

#!/usr/bin/env dart

Seems to be the way. It will look for dart binaries in the user's environment, and apparently enables simple Dart scripts to be executed from anywhere, provided the Dart VM is installed and in the $PATH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top