How to export variable for use with sudo?

You may use sudo's -E option:

FMPEG=yes sudo -E sbopkg -B -i vice

From the manual:

-E, --preserve-env

Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.

Note that this exports all your existing environment variables. It is safer to only export the environment variables you need with the following syntax:

sudo FMPEG=yes sbopkg -B -i vice


sudo sanitizes the variables in the environment before invoking the given command. You will have to ask it to preserve the variables in the environment for your command to work.

Additionally, you will have to export the FMPEG variable before using sudo (export FMPEG=yes; sudo), assign it in the same go as invoking sudo (FMPEG=yes sudo), or use env (env FMPEG=yes sudo).

To preserve the environment variables, use -E (or --preserve-env):

$ env FMPEG=yes sudo -E sbopkg -B -i vice

It's also possible to set specific environment variables like this:

$ sudo FMPEG=yes sbopkg -B -i vice

If this fails due to the security policy in place, bring up a root shell and set the variable there:

$ sudo -s
# env FMPEG=yes sbopkg -B -i vice
# exit

note that: (export a=b; command) is equivalent to a=b command. Note the brackets.

Then if we apply the variable not to sudo but direct to sbopkg, so instead of FFMPEG=yes sudo sbopkg -B -i vice we can do sudo FFMPEG=yes sbopkg -B -i vice

If a security police prevent this, then: sudo bash -c "FFMPEG=yes sbopkg -B -i vice"

(Don't use -E, it us insecure as it will leak all sorts of unexpected variables.)