27

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
  • 3,926
  • 3
  • 31
  • 37
  • 8
    It's easy when you know that #! is called "shebang", as you could see from comments I did not know about that.. – Nulldevice Mar 30 '11 at 13:32
  • Note 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. – glenn jackman Mar 30 '11 at 17:21
  • 1
    The 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. – Bogdan Calmac Mar 14 '14 at 18:35

3 Answers3

58

This one #!/usr/bin/env groovy
will search your path looking for groovy to execute the script

jpertino
  • 2,179
  • 13
  • 9
  • 4
    A limitation is that you can't give groovy any arguments (like classpath) when using a shebang on linux. See [this answer](http://stackoverflow.com/a/8945888/54396). – Patrick Jan 20 '12 at 18:40
  • It seems to me that you can't go wrong by sticking with `#!/usr/bin/env `, as is the case with `#!/usr/bin/env python` as well. ;) – Chiramisu Oct 31 '19 at 19:03
10

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"
  1. The first line is a shebang (#!) that tells the OS to run the script as a regular shell script.
  2. 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)
  3. 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"
  1. Again, the shebang signals the OS to start executing this files as a shell script.
  2. The shell parses '''': as two empty strings '' followed by a colon, which is a no-op.
  3. The shell will execute the rest of the file, line by line, until it find an exec or an exit
  4. If everything is ok, the shell will run the Groovy command on the script file itself ("$0")
  5. 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.
Tobia
  • 17,856
  • 6
  • 74
  • 93
  • I 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 – Andreas M. Oberheim Sep 11 '18 at 16:56
6

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

Neuron
  • 5,141
  • 5
  • 38
  • 59
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • thank! It was known to me that combination #! has special name, but I failed to remember it. – Nulldevice Mar 30 '11 at 11:24
  • If it helps you remember, the starting symbols for the line are a "sharp" **#** and an interrobang **!**, hence the term "shebang". – mfortner Feb 08 '15 at 00:04
  • @mfortner A good mnemonic, but the exclamation point is a bang, not an interrobang. An [interrobang](https://en.wikipedia.org/wiki/Interrobang) is the combination exclamation point/question mark symbol (‽). – M. Justin Feb 07 '17 at 15:37