If it is an sh
script - as in, it explicitly references #!/bin/sh
- which might still be bash
but would be like invoking it with --posix --no-rc --no-profile
- then you can specify the ENV
file with the ENV
environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV
- or else with export
. For example, for $PATH
you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV
variable you reference is not a file you need to source - and it isn't interpreted anyway when bash
is invoked as sh
- but is rather a path to a file that is sourced when a non-interactive bash
shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env
utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env
to invoke the /usr/bin/bash
command - with all its arguments appended - with the $BASH_ENV
environment variable specified, but otherwise with a clean environment entirely.
Create a wrapper shell function sshc
which prefixes the source ~/.bash_profile
boiler plate for you:
function sshc {
local host=$1
local cmd=$2
ssh $host "source ~/.bash_profile && $cmd"
}
You can then use this as:
$ sshc localhost 'which perl'
/home/calid/perl5/perlbrew/perls/perl-5.20.1/bin/perl
Best Answer
I agree it's not very clear.
1. At shell startup,
if the
_
variable was in the environment thatbash
received, thenbash
leaves it untouched.In particular, if that
bash
shell was invoked by anotherbash
shell (thoughzsh
,yash
and someksh
implementations also do it), then thatbash
shell will have set the_
environment variable to the path of the command being executed (that's the 3rd point in your question). For instance, ifbash
is invoked to interpret a script as a result of anotherbash
shell interpreting:That
bash
will have passed_=/path/to/bash-scrip
in the environment given tobash-script
, and that's what the initial value of the$_
bash
variable will be in thebash
shell that interprets that script.Now, if the invoking application doesn't pass a
_
environment variable, the invokedbash
shell will initialise$_
to theargv[0]
it receives itself which could bebash
, or/path/to/bash
or/path/to/some-script
or anything else (in the example above, that would be/bin/bash
if the she-bang of the script was#! /bin/bash
or/path/to/bash-script
depending on the system).So that text is misleading as it describes the behaviour of the caller which
bash
has no control over. The application that invokedbash
may very well not set$_
at all (in practice, only some shells and a few rare interactive applications do,execlp()
doesn't for instance), or it could use it for something completely different (for instanceksh93
sets it to*pid*/path/to/command
).2. Subsequently
The Subsequently is not very clear either. In practice, that's as soon as
bash
interprets a simple command in the current shell environment.In the case of an interactive shell, that will be on the first simple command interpreted from
/etc/bash.bashrc
for instance.For instance, at the prompt of an interactive shell:
For a non-interactive shell, it would be the first command in
$BASH_ENV
or of the code fed to that shell if$BASH_ENV
is not set.3. When Bash executes a command
The third point is something different and is hinted in the discussion above.
bash
, like a few other shells will pass a_
environment variable to commands it executes that contains the path thatbash
used as the first argument to theexecve()
system calls.4. When checking mail
The fourth point is described in more details in the description of the
MAILPATH
variable:Example: