Ruby on Rails script console

I was not able to run ./script/console earlier, and it used an error since my script console file included #!/usr/bin/env ruby19 . After running the test and checking, I fixed this error by replacing #!/usr/bin/env ruby19 with #!/usr/bin/env ruby .

What does the above line do?

Versions:

  • Ruby: 1.9.2-p180
  • Ruby on Rails: 2.3.5
+3
source share
2 answers

#! (hashing) in the first line of the text file tells the program loader on most * nix systems to call the program specified below (in this case, /usr/bin/env ), with any parameters specified (in this case, ruby ).

/usr/bin/env is just a portable way to search your environment for the program named in the first argument. Here is the Ruby interpreter. If the Ruby interpreter is in your PATH, env will find it and run it using the rest of the file as input.

You probably did not have a program named ruby19 in your PATH, so you would get an error. You have a program called ruby , so it works.

+6
source

The shebang line in the Unix script should indicate the full path, so this is:

 #!/usr/local/bin/ruby 

but this is not so:

 #!ruby 

The problem with the first form is that you need to use the full path, but the actual path will not be the same for all systems. The env utility is often used to allow a script to look for the PATH environment variable for the corresponding interpreter, env should always be in /usr/bin/env so that you can safely use this as the full path, and then allow env find PATH for the named interpreter.

From the exact guide :

env [-i] [name=value]... [utility [argument...]]

The env utility should get the current environment, change it according to its arguments, and then call the utility, called the operand of the utility, with the changed environment.

This is not very useful in your case, but I decided that I should include it anyway. Using shebang env is a hack bit that does not use the env supposed behavior.

+1
source

All Articles