Shebang and Groovy
19,107
Solution 1
This one #!/usr/bin/env groovy
will search your path looking for groovy to execute the script
Solution 2
A common trick is to write a script that has meaning in more than one language, also known as a "polyglot" script.
In the case of Bash and Groovy, this is particularly easy:
#!/bin/sh
//bin/true; exec groovy -cp .. "$0"
println "Hello from Groovy"
- The first line is a shebang (
#!
) that tells the OS to run the script as a regular shell script. - The second line, when executed by the shell, invokes the
/bin/true
command (a no-op); then finds the groovy executable in the PATH and runs it on the script file itself ("$0"
) plus additional arguments, replacing the current shell process (exec
) - Groovy will ignore the first line, because it's a shebang; it will ignore the second line because it's a comment (
//
) and will run the rest of the script.
If you need a more elaborate shell part, maybe to set up environment variables, or discover where Groovy is installed, you can use a different trick:
#!/bin/sh
'''':
echo Hello from Shell
exec groovy -cp .. "$0"
'''
println "Hello from Groovy"
- Again, the shebang signals the OS to start executing this files as a shell script.
- The shell parses
'''':
as two empty strings''
followed by a colon, which is a no-op. - The shell will execute the rest of the file, line by line, until it find an
exec
or anexit
- If everything is ok, the shell will run the Groovy command on the script file itself (
"$0"
) - Groovy will skip the shebang line, then it will parse
'''':
as the beginning of a long string'''
, thus skipping all the shell commands, and then run the rest of the script.
Solution 3
According to this you can use #!/usr/bin/groovy
(if that's its location). The search term you are looking for is shebang
(which is what that first line is called).
Related videos on Youtube
Comments
-
Nulldevice almost 4 years
Is it possible to declare at the start of a file that it should be executed as a Groovy script?
Examples for other scripting languages:
#!/bin/sh #!/usr/bin/python #!/usr/bin/perl
-
Nulldevice about 13 yearsIt's easy when you know that #! is called "shebang", as you could see from comments I did not know about that..
-
glenn jackman about 13 yearsNote that the first word on the shebang line has to be a binary executable. If it happens that your "groovy" program is a shell script, you have to use the
#!/usr/bin/env groovy
form. -
Bogdan Calmac about 10 yearsThe funny thing is that googling it returns exactly this page. stack overflow is actually the best resource, even for simple questions like this one. Other people will come directly here and get an authoritative answer rather than browsing through different blog posts.
-
-
Nulldevice about 13 yearsthank! It was known to me that combination #! has special name, but I failed to remember it.
-
Patrick over 12 yearsA limitation is that you can't give groovy any arguments (like classpath) when using a shebang on linux. See this answer.
-
mfortner about 9 yearsIf it helps you remember, the starting symbols for the line are a "sharp" # and an interrobang !, hence the term "shebang".
-
M. Justin about 7 years@mfortner A good mnemonic, but the exclamation point is a bang, not an interrobang. An interrobang is the combination exclamation point/question mark symbol (‽).
-
Andreas M. Oberheim over 5 yearsI tried all other solutions. This is the only one that worked inside a Windows 7 git bash installation on a 64-bit machine with very restricted rights
-
Chiramisu over 4 yearsIt seems to me that you can't go wrong by sticking with
#!/usr/bin/env <environment>
, as is the case with#!/usr/bin/env python
as well. ;)