$cat test
export var=value

$source ./test

$cat print.sh
echo $var

$chmod +x print.sh

$echo $var
value

$sudo ./print.sh

I get nothing. I was expecting to see value.

What is going on?

link|improve this question

Does $ ./print.sh (without sudo) shows value ? – Didier Trosset Oct 27 '11 at 11:55
feedback

migrated from stackoverflow.com Oct 28 '11 at 2:06

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 5 down vote accepted

if you want to keep the environment with a sudo, use sudo -E that will preserve environment.

If you need to always preserve some of your environment variables, edit the sudoers file to add lines :

Defaults        env_reset
Defaults        env_keep += "YOURVARIABLE OTHERVARIABLE YETANOTHER"
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.