Bash – Environment variable set but not respected

bashenvironment-variables

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)

Best Answer

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

export EDITOR=vim
Related Question