3

I know that similar questions have been asked previously, but how do I go about building a Ruby file that can be run in the terminal without typing "ruby" before it?

The end goal here is to create a command line toolkit-type thing. Right now, in order to do what I want the user to be able to do, they must type

ruby Cherry init file_name

into the terminal.

What I would like, is the following:

Cherry init file_name

Is there anything I can add to the ruby file itself that allows it to be run with Ruby automatically, just by calling the name of the file? If not, how would I go about doing that? Any and all help is appreciated, as the other similar threads had no answers which I could understand.

Lloyd7113
  • 187
  • 3
  • 12
  • Check this: https://stackoverflow.com/questions/8721369/how-to-execute-a-ruby-script-in-terminal – iGian Jan 20 '19 at 19:48

1 Answers1

5

Make sure that your script has a correct shebang line, e.g. something like this:

#!/usr/bin/env ruby

Also, make sure that the user trying to run the script has read and execute permissions for the script file.

That is all you need to do. Assuming you have a Ruby execution engine installed, and its executable is named ruby and it is in your $PATH, then you can execute your script file like any other executable.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • "Execute permissions" can be set with `chmod +x script_name`. – tadman Jan 20 '19 at 20:27
  • Note that the current directory is generally *not* in `$PATH` (and shouldn't be). That means you need to put the script in a suitable binaries directory. `/usr/local/bin/` is good, or you can make your own personal binaries directory and add it to your `$PATH`. – Gordon Davisson Jan 20 '19 at 21:26
  • Or run the script with `./scriptpath`. That still runs it without typing `ruby` – Max Jan 21 '19 at 14:38