56

When I did which groovy, I got the below output:

/usr/local/bin/groovy

So I went ahead and created a helloworld.groovy with the below content

#!/usr/local/bin/groovy
println "hello world"

After that I did chmod +x helloworld.groovy and attempted to run the file with ./hellworld.groovy and sadly, I got this error ./helloworld.groovy: line 2: print: command not found

I could get rid of the error by changing to

#!/usr/bin/env groovy
println "hello world"

Why would the first method cause the error?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jjennifer
  • 1,285
  • 4
  • 12
  • 22
  • 1
    There's a great discussion on this topic in this answer http://stackoverflow.com/questions/306139/how-do-i-include-jars-in-a-groovy-script/8945888#8945888 – chim Oct 05 '16 at 07:37
  • And also on the same question... http://stackoverflow.com/a/9692013/673282 – chim Oct 05 '16 at 07:38

4 Answers4

84

You need to run the script like this:

groovy helloworld.groovy
grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66
  • 11
    or just `groovy helloworld`. :) – dmahapatro Sep 11 '13 at 04:21
  • 8
    since groovy is also a scripting language, we should be able to run it like perl or python right? so the question is why `./helloworld.groovy` returns an error about the `println`. what is the difference between `#!/usr/local/bin/groovy` and `#!/usr/bin/env groovy`? – jjennifer Sep 11 '13 at 13:13
  • 14
    `#!/usr/local/bin/groovy` is the exact location where groovy is installed. That makes the script not portable to another system where groovy is installed elsewhere. This however `#!/usr/bin/env` groovy calls the the program "/usr/bin/env" with the argument "groovy", and /usr/bin/env is searching your PATH variable to call "groovy".
    BTW: the better shebang is `#! /usr/bin/env`. Note the space between ! and /. That way the first 4 bytes of every "Script" are identical (that is old school to make this the "magic number" of the file)
    – Angel O'Sphere Dec 12 '13 at 12:25
  • 1
    @angel-osphere Why is it better to have the first 4 characters identical? Is there any real benefit? (e.g., does it work better under different shells/ un*x environments, etc?) – DrUseful Mar 05 '18 at 11:06
  • 2
    @DrUseful maybe if you're running on a PDP-11, lol. Otherwise, I don't think it matters. (Magic numbers are 8-bytes anyway, and modern (past 10 years at least) UNIX exec() functions only look for the 2 bytes #! to determine if it's a script or not) – Isaac Freeman Jul 19 '19 at 14:06
0
#!/bin/sh
sed '1,2d' "$0"|$(which groovy) /dev/stdin; exit;

println("hello");
dieter
  • 1,213
  • 11
  • 17
0
#!groovy
println("hello world!")
$ chmod +x script.groovy
$ ./script.groovy
SP QRT
  • 95
  • 5
-1

It will work on Linux kernel 2.6.28 (confirmed on 4.9.x). It won't work on FreeBSD and other Unix flavors.

Your /usr/local/bin/groovy is a shell script wrapping the Java runtime running Groovy.

See the Interpreter Scripts section of EXECVE(2) and EXECVE(2).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131