简体   繁体   中英

Different sudo environment variables between ruby and shell

I'm writing a script that needs to be run with sudo, and have run into something curious and wondered if anyone could explain it...

My environment variable seems not to behave the same when in ruby vs when in shell:

user@server:~$ sudo echo $HOME
/home/user

user@server:~$ sudo /opt/ruby-2.1.2-p95/bin/irb
irb(main):002:0> puts ENV['HOME']
/root
=> nil
irb(main):003:0>

What could be the cause of this?

Edit: To clarify - I'm not asking why sudo is resetting my env variable, I'm asking why it does't when I run shell commands/scripts (1st command), but it does when I run ruby (2nd command) from the same shell session without any change in configuration.

By default, sudo resets your environment variables. You might be able to use sudo's --preserve-env (or -E for short) to preserve the environment. You can run man sudo to learn more about sudo and its options.

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