Bash – How to discard stderr in restricted Bash shell

bashio-redirectionrestricted-shell

In my .bashrc there are a couple commands which redirect standard error to /dev/null, and this is not allowed within rbash:

bash: /dev/null: restricted: cannot redirect output

Is there some way to get around this (short of modifying Bash)? That is, to either

  • discard standard error some other way or
  • only attempt to redirect if it's allowed.

Best Answer

You can close stderr as in:

ls /blah 2>&-

While that will work in most cases, it may cause problems as poorly written applications may end up opening a new file which would automatically get 2 as the file descriptor and could end up writing error messages where you wouldn't want them to.

That also means that any write to stderr done by the application would return with an error (EBADF) which may affect their behavior.

As pipes are allowed, you could provide with a command that discards all its input. Here, using grep -v '^' (but you could use tail -n 0 or sed d or a script called discard that does cat > /dev/null):

{ ls /blah 2>&1 >&3 | grep -v '^'; } 3>&1

Alternatively, you could have whatever starts that restricted shell start it with the fd 3 (for instance) redirected to /dev/null (rbash 3> /dev/null), so you can then do within the restricted shell:

ls /blah 2>&3

Which is allowed:

$ rbash -c 'ls /blah 2>&3' 3> /dev/null
$

You can check whether redirection to /dev/null is allowed/works or not with:

if true 2>&- > /dev/null; then
  echo allowed
fi

You can check whether the shell is restricted or not with:

case $- in
  (*r*) echo restricted
esac
Related Question