简体   繁体   中英

Why does ruby's '$:' or '$LOAD_PATH' change when I run it with sudo?

I am trying to run a ruby script as root. When I try to require a gem, ruby says it can't be found. This is because of $:

$ ruby -e "puts $:"
/opt/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.0.beta4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/cgi_multipart_eof_fix-2.5.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/daemons-1.1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/ext
/opt/local/lib/ruby/gems/1.9.1/gems/fastthread-1.0.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/gem_plugin-0.2.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/bin
/opt/local/lib/ruby/gems/1.9.1/gems/json_pure-1.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/bin
/opt/local/lib/ruby/gems/1.9.1/gems/mongrel-1.1.5/lib
/opt/local/lib/ruby/gems/1.9.1/gems/msgpack-0.4.3/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nestful-0.0.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/bin
/opt/local/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.2/lib
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/ext
/opt/local/lib/ruby/gems/1.9.1/gems/num_to_bytes-1.4.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/open4-1.0.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby-processing-1.0.9/lib
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/ruby_parser-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/bin
/opt/local/lib/ruby/gems/1.9.1/gems/rubyforge-2.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sexp_processor-3.0.4/lib
/opt/local/lib/ruby/gems/1.9.1/gems/sinatra-1.0/lib
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/bin
/opt/local/lib/ruby/gems/1.9.1/gems/thin-1.2.7/lib
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.
$ sudo ruby -e "puts $:"
/opt/local/lib/ruby1.9/site_ruby/1.9.1
/opt/local/lib/ruby1.9/site_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/site_ruby
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1
/opt/local/lib/ruby1.9/vendor_ruby/1.9.1/i386-darwin10
/opt/local/lib/ruby1.9/vendor_ruby
/opt/local/lib/ruby1.9/1.9.1
/opt/local/lib/ruby1.9/1.9.1/i386-darwin10
.

Why is this happening?

Try sudo -E ruby -e "puts $:" .

From the sudo man page:

   -E          The -E (preserve environment) option will override the env_reset option in sudoers(5)).
               It is only available when either the matching command has the SETENV tag or the setenv
               option is set in sudoers(5).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM