My tests show that r
appears in $-
after rbash
reads ~/.bashrc
, as if the shell was not restricted during processing the file. This corresponds to what the manual says about restrictions:
These restrictions are enforced after any startup files are read.
One of the restrictions is the inability to change PATH
. Since you do want to change PATH
, it's actually good for your case the restrictions are enforced later. It seems r
in $-
indicates that the shell is effectively restricted at the moment. You want to detect if it's going to be effectively restricted. You cannot use $-
for this.
There is a mechanism designed deliberately for what you need to do. It's in shopt
[emphasis mine]:
restricted_shell
The shell sets this option if it is started in restricted mode […]. The value may not be changed. This is not reset when the startup files are executed, allowing the startup files to discover whether or not a shell is restricted.
Use it like this:
shopt -q restricted_shell && PATH='' && echo 'restricted' || echo 'unrestricted'
Or better:
if shopt -q restricted_shell; then
PATH=''
echo 'restricted'
else
echo 'unrestricted'
fi
The latter is better because the second echo
depends only on the exit status from shopt
. For comparison: in the former snippet (and in your original code) failing first echo
would trigger the second echo
. With echo
s this seems marginal, irrelevant or harmless, but if you add more commands then a line with many &&
and ||
may surprise you if one of them fails unexpectedly.
Also note that printing anything from .bashrc
will break scp
and similar software (even printing to stderr may), unless your .bashrc
is smart enough.
PATH
in restricted bash: gnu.org/software/bash/manual/html_node/… (But you can probably useenv
to start the restricted bash with a differentPATH
.)PATH
from within.bashrc
processed automatically by a restrictedbash
. The restrictions kick in later.