0

I have a follow-up question regarding Shell out from ruby while setting an environment variable. I try to feed the environment variable to a shell script via sudo, but not successful.

This is the sample one-line script, "/usr/bin/wrapper.sh".

echo $MYVAR

Test,

$ irb
> system({'MYVAR' => "42"}, "echo $MYVAR")        ==> GOOD
> system({'MYVAR' => "42"}, "wrapper.sh")         ==> GOOD
> system({'MYVAR' => "42"}, "sudo echo $MYVAR")   ==> GOOD
> system({'MYVAR' => "42"}, "sudo wrapper.sh")    ==> NOT GOOD

Thanks for the help.

1
  • I'm not sure if there's a way to flag that variable as export so it affects subshells. wrapper.sh is not running in the same process as sudo's immediate shell. Does ENV['MYVAR'] = 42 before running it propagate properly? Commented Apr 3, 2017 at 19:04

1 Answer 1

1

you need sudo -E

$ irb
>> system({'MYVAR' => "42"}, "sudo wrapper.sh")
=> true
>> system({'MYVAR' => "42"}, "wrapper.sh")
42
=> true
>> system({'MYVAR' => "42"}, "sudo -E wrapper.sh")
42
=> true
>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.