Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shebang line in Dart scripts (the portable way)?

Tags:

shebang

dart

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?

like image 772
Diti Avatar asked Oct 21 '25 21:10

Diti


1 Answers

(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.

like image 119
Diti Avatar answered Oct 26 '25 19:10

Diti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!