Pass GLOBIGNORE to bash call

The page bash

reports

If the shell is started with the effective user (group) id not equal to
the real user (group) id, [...] the  SHELLOPTS,  BASHOPTS, CDPATH, and
GLOBIGNORE variables if they appear in the environment, are ignored

      

This usually happens.

> export GLOBIGNORE='*t*'
> echo *
afile
> bash -i
>> # look, the variable is passed through
>> $ echo $GLOBIGNORE
*t*
>> # but to no effect
>> $ echo *
afile anotherfile athirdfile

      

I don't think it would be wise to spoof the real user ID to enable transmission GLOBIGNORE

and a number of other unwanted side effects.

Is it possible to force the subshell to respect the exported one GLOBIGNORE

?

+3


source to share


1 answer


Some other hackers can come to the rescue. All of these solutions require at least modification to the shell invocation, but be prepared to get started in parentheses.

Since shell startup is different in interactive shells, two strategies are needed.

Interactive

When starting an interactive session, bash will usually output the file ~/.bashrc

by default. There is a switch to change where to find this file. This can be used losslessly if the passed script redirects to the original location.

> echo 'GLOBIGNORE=*t*' > rc
> echo 'source ~/.bashrc' >> rc
> bash --rcfile rc -i
>> echo *

      

Non-interactive, modifiable command line

As Cyrus already pointed out , one can simply enlarge the command with the assignment so that it starts inside a subshell.



> bash -c 'GLOBIGNORE="*t*" ; echo *'

      

Fully automated

Another special variable can be used to avoid modifying passed commands. It is named BASH_ENV

and denotes the source script for the source when starting a non-interactive session. In this case, a similar strategy arises --rcfile

.

> echo 'GLOBIGNORE=*t*' > rc
> BASH_ENV=rc bash -c "echo *"

      

Or, to be even more sloppy and to avoid a temporary file rc

, we can force a pipeline that is not explicitly intended as the value is -

not treated as standard input.

> echo 'GLOBIGNORE=*t*' | BASH_ENV=/dev/stdin bash -c "echo *"

      

0


source







All Articles