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

In /etc/bash.bashrc I have:


And it is indeed set:

lev@home ~ $ echo $EDITOR

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

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)

2 Answers 2


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

export EDITOR=vim
  • 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? Commented Dec 13, 2012 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
    Commented Dec 13, 2012 at 11:14
  • That's good to know, but visudo still behaves the same way. Is there another reason for that? Commented Dec 13, 2012 at 12:09

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.

  • How is the sudo environment formed? Isn't /etc/bash.bashrc read? Why do I see vim when I do sudo echo $EDITOR then? Commented Dec 13, 2012 at 15:56
  • i better way to test is sudo env | grep EDITOR then sudo -E env | grep EDITOR
    – llua
    Commented Dec 13, 2012 at 16:46
  • man sudo | less -p '^COMMAND EXECUTION' explains what happens better than what i can.
    – llua
    Commented Dec 13, 2012 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? Commented Dec 13, 2012 at 18:32
  • 1
    @LevLevitsky $EDITOR is expanded by your current shell, using sudo won't change the result.
    – Chris Down
    Commented Dec 13, 2012 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .