Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I find the following behavior a little confusing, can someone please explain why it happens?

In /etc/bash.bashrc I have:

EDITOR=vim

And it is indeed set:

lev@home ~ $ echo $EDITOR
vim

I would like visudo to respect that. Now, I have read in man visudo that it doesn't always respect this variable, but then I don't understand why the following gives different results:

$ sudo visudo # opens vi
$ sudo EDITOR=vim visudo # opens vim

Note that the EDITOR variable must be set for root, too (AFAIU):

$ sudo echo $EDITOR
vim

Also, when I install packages from AUR using yaourt (I'm on Arch Linux) and opt to edit the PKGBUILD file, I see:

Please add $EDITOR to your environment variables
for example:
export EDITOR="vim" (in ~/.bashrc)
(replace vim with your favorite editor)

==> Edit PKGBUILD with:

So the issue is not limited to visudo. Why can I see the variable set, but programs can't (unless I specify it again right in the command)?

Technical info:

lev@home ~ $ uname -a
Linux home 3.6.9-1-ARCH #1 SMP PREEMPT Tue Dec 4 08:04:10 CET 2012 x86_64 GNU/Linux
lev@home ~ $ bash --version | head -1
GNU bash, version 4.2.39(2)-release (x86_64-unknown-linux-gnu)
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You've set it, but not exported it. Change the line to this:

export EDITOR=vim
share|improve this answer
    
I tried that, ran source /etc/bash.bashrc, nothing changed (for visudo at least). What is supposed to be the difference? I thought if the files are sourced, it's the same with and without export, am I wrong? –  Lev Levitsky Dec 13 '12 at 11:13
2  
@LevLevitsky You're mistaken. Exporting a variable makes it part of the shell's environment, and thus, is propagated to children upon fork(). Without export, that doesn't happen (the variable exists in the shell, but is not propagated to its children). –  Chris Down Dec 13 '12 at 11:14
    
That's good to know, but visudo still behaves the same way. Is there another reason for that? –  Lev Levitsky Dec 13 '12 at 12:09
add comment

After following Chris's answer to export the variable into the environment for child processes to inherent, you need to tell sudo to preserve your current environment. This can be done with sudo's -E option which will preserve the entire environment; or in this specific case, add EDITOR to the env_keep variable in your sudoers file to preserve just that variable.

share|improve this answer
    
How is the sudo environment formed? Isn't /etc/bash.bashrc read? Why do I see vim when I do sudo echo $EDITOR then? –  Lev Levitsky Dec 13 '12 at 15:56
    
i better way to test is sudo env | grep EDITOR then sudo -E env | grep EDITOR –  llua Dec 13 '12 at 16:46
    
man sudo | less -p '^COMMAND EXECUTION' explains what happens better than what i can. –  llua Dec 13 '12 at 16:51
    
sudo -E visudo opens vim. There is indeed no EDITOR in sudo env... Where does it read it from when I do sudo echo $EDITOR then? –  Lev Levitsky Dec 13 '12 at 18:32
    
@LevLevitsky $EDITOR is expanded by your current shell, using sudo won't change the result. –  Chris Down Dec 13 '12 at 21:59
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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