How do I make a Ruby script into a bash command?

Doug Smith picture Doug Smith · Jan 19, 2015 · Viewed 7.7k times · Source

I have a Ruby file, and I run it as ruby file.rb "parameters". I prefer to run it as regtask parameters without having to include ruby and the filename every time. I want it to be on the same level as ls. How would I accomplish this?

Answer

Amadan picture Amadan · Jan 19, 2015

Edit your file, make sure this is the first line, so your system knows how to execute your file:

#!/usr/bin/env ruby

Next, change the file's permissions to make it executable:

chmod a+x file.rb

And finally, rename it and move it somewhere where it will be executed without having to write its full path:

mkdir -p ~/bin
mv file.rb ~/bin/regtask

(Most systems will automatically add ~/bin to PATH if it exists; if not, you will have to add it to PATH yourself in your startup files.)