On Thursday, May 25, 2017 at 7:15:21 AM UTC+8,
hongy...@gmail.com wrote:
> On Thu, 25 May 2017 00:08:00 +0200, Helmut Waitzmann wrote:
>
> >> $ sudo PATH="$PATH" which stow
> >> /home/werner/software/software-management/stow/stow.git/bin/stow
> Another issue is as follows:
>
> If I want to set two or more variables, then the following form will fail:
>
> sudo var1="$var1" var2="$var2" some_command_here
>
> See my following testings:
> $ export PERL5LIB=/home/werner/software/software-management/stow/stow.git/
> lib:$PERL5LIB
> $ export PATH=/home/werner/software/software-management/stow/stow.git/bin:
> $PATH
> $ sudo PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
> sudo: stow: command not found
Sorry to continue replying to this topic after so long. The above problem should have nothing to do with sudo. It just indicates that the utility stow doesn't exist on the PATH.
> While all of the following forms will be ok:
>
> $ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" stow -h
This one is wrong. The semicolon will terminate the sudo command.
> $ sudo PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
Correct.
> $ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" bash -c 'stow -h'
Wrong as noted above.
> $ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
> $ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
Wrong. These commands will executed using the environment variables expanded from the current shell instead of the ones living in the forked process by sudo.
> $ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
> $ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
Correct.
To summarize, based on tries, all the following forms are correct:
$ sudo var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo env var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo -E var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo -E env var1=val1 var2=val2 bash -c 'command want to be excluded'
Best,
HY