# A Rake executable script, is this the correct approach

**URL:** <https://www.ruby-forum.com/t/a-rake-executable-script-is-this-the-correct-approach/73627>\
**Category:** Ruby\
**Created:** [October 25, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/a-rake-executable-script-is-this-the-correct-approach/73627 "2006-10-25T20:50:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 25, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/a-rake-executable-script-is-this-the-correct-approach/73627/1 "2006-10-25T20:50:29Z")

</div>

I humbly come to you, because searching, mostly google, has failed to  
produce a reference, or someone who has done this.

We traditionally run Rakefile’s using either  
$ rake  
or  
$ rake -f my.rake  
etc

what if I wanted an executable script, say myscript, which is really a  
rake task file, but runs as though it where run as above.  
Attempt 1  
#!/usr/bin/env rake  
task :default do puts “Hello, Rake!” end  
FAILED

Attempt 2  
#!/usr/bin/env ruby  
task :default do puts “Hello, Rake!” end  
Rake::Task[:default].invoke  
SUCCESS

So, is the latter really the best way? While it works I worry that rake  
is somehow not setup the same way if the this had been run the  
traditional way. Thanks

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 25, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/a-rake-executable-script-is-this-the-correct-approach/73627/2 "2006-10-25T21:06:31Z")

</div>

Well, attempt 1 is merely defining the task, the task then has to be  
run,  
which is what’s happening in Attempt 2.

When you type `rake` in the command line, what you are really doing is

rake default

Hopefully that explains what’s going on.

Jason

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 25, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/a-rake-executable-script-is-this-the-correct-approach/73627/3 "2006-10-25T21:42:39Z")

</div>

You missed the point and the sha-bang in attempt 1 which was  
#!/usr/bin/env rake, not ruby.

I understand what rake is doing, what I want is an executable rake file  
without having to explicitly run rake. Thanks
