0

When I run a perl script pdfannotextractor.pl in bash, I want to set the value of a variable TEXMFVAR needed by the script, and pass it to the script.

I googled around, and thought I found a solution:

$ TEXMFVAR=/usr/local/texlive/2014/texmf-var sudo /usr/local/texlive/2014/texmf-dist/scripts/pax/pdfannotextractor.pl --install

But it seems TEXMFVAR is still empty in the script, because it creates a directory $TEXMFVAR under my current dir. I am puzzled.

$ ls \$TEXMFVAR/*
$TEXMFVAR/ls-R

$TEXMFVAR/scripts:
pax

Do I need to export the variable?

Is this a problem of usage of environment variables?

Note: My original problem is about texlive and here https://tex.stackexchange.com/questions/243889/error-installing-pdfbox-library-for-pax-package

  • I'd suggest condensing the problem to A=/tmp sudo echo "$A". Should I edit? – Anko May 8 '15 at 19:19
  • 1
    @Anko: In your suggested command, $A is expanded by the shell before sudo is invoked; it won't necessarily reflect the sudo and echo commands' actual environment. You can try A=/tmp sudo printenv | grep '^A=' – Keith Thompson May 8 '15 at 19:55
5

sudo sanitizes the environment so potentially harmful variables are not passed to the process running as the superuser. You can change this behavior with the -E or --preserve-env flag to sudo.

  • Thanks. That is right. Can this be done by export? – Tim May 8 '15 at 19:26
  • 2
    @Tim export is a shell builtin and has nothing to do with what sudo does to the environment. Only sudo's -E option affects that. export is not relevant here. – Celada May 8 '15 at 19:35
  • @Celada What I meant is that does export TEXMFVAR=/usr/local/texlive/2014/texmf-var && sudo -E /usr/local/texlive/2014/texmf-dist/scripts/pax/pdfannotextractor.pl --install work the same as TEXMFVAR=/usr/local/texlive/2014/texmf-var sudo -E /usr/local/texlive/2014/texmf-dist/scripts/pax/pdfannotextractor.pl --install ? – Tim May 8 '15 at 19:41
  • @Tim How you put TEXMFVAR into the environment is unimportant. In both of your examples it will be in the environment that is passed to sudo. The only difference is the version with export will have TEXMFVAR in your environment still after sudo returns, while the second version will not. – casey May 8 '15 at 19:43
  • @Tim sure, that will work too, but that command does something different! Instead of setting $TEXMFVAR just for the single sudo command it puts it in the shell's environment and leaves it there. – Celada May 8 '15 at 19:44
3

With sudo, you can set variable for command in form var=value, try:

sudo TEXMFVAR=/usr/local/texlive/2014/texmf-var \
/usr/local/texlive/2014/texmf-dist/scripts/pax/pdfannotextractor.pl --install

-E option won't work with some variables like LD_LIBRARY_LOAD or LD_PRELOAD for security reason.

1

If for some reason sudo is rejecting the environment variables "directly" specified per cuonglm (perhaps because of policy reasons), you can just use env ... assuming you have sudo policy to use it.

sudo env TEXMFVAR=/usr/local/texlive/2014/texmf-var /usr/local/texlive/2014/texmf-dist/scripts/pax/pdfannotextractor.pl --install
  • It's the same with var=value, env is useless here. – cuonglm May 8 '15 at 20:34
  • Why is it uselss? I mean if var=value doesn't work, it's because sudo has restricted it. Then again, if it's restricted that, what are the chances of running env? – Otheus May 8 '15 at 20:45
  • sudo does sanity check for environment variable before executing command. var=value or env var=value both run after that sanity check, they have the same effect – cuonglm May 8 '15 at 20:54
  • No, not in my case. In my case sudo is executing the command env and the TEXMFVAR is an argument to env, not sudo. – Otheus May 8 '15 at 21:08
  • That's exactly what I said. Just omitting env and everything work well. – cuonglm May 8 '15 at 22:13

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