Ruby on Rails script console
I wasn't able to run ./script/console
previously, and it used to throw an error since my script console
file had included #!/usr/bin/env ruby19
. After doing hit and trial 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-p开发者_开发百科180
- Ruby on Rails: 2.3.5
The #!
(hash bang) in the first line of a text file tells the program loader in most *nix systems to invoke the program that is specified next (in this case, /usr/bin/env
) with any params supplied (in this case, ruby
).
/usr/bin/env
is just a portable way of looking in your environment for program named in the first argument. Here it 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 didn't have a program named ruby19
in your PATH, so you'd get the error. You do have a program named ruby
, so that works.
The shebang line in a Unix script is supposed to specify a full path, so this:
#!/usr/local/bin/ruby
is valid but this is not:
#!ruby
The problem with the first form is that, well, you need to use a full path but the actual path won't be the same on all systems. The env
utility is often used to allow a script to search the PATH
environment variable for the appropriate interpreter, env
should always be in /usr/bin/env
so you can safely use that as a full path and then let env
search the PATH
for the named interpreter.
From the fine manual:
env [-i] [name=value]... [utility [argument...]]
The env utility shall obtain the current environment, modify it according to its arguments, then invoke the utility named by the utility operand with the modified environment.
That isn't terribly helpful in your case but I figured I should include it anyway. The shebang use of env
is a bit of a hack that doesn't use the intended behavior of env
.
精彩评论