I want to pass an entire code block to unshare, but I don't see an option to pass a function directly. I currently have a solution with two files:

File 1: run_with_bash_as_default_shell.sh

sudo unshare -m "$( which run_with_bash_as_default_shell_unsafe.sh )" "$@"

File 2: run_with_bash_as_default_shell_unsafe.sh

sudo mount --bind /bin/bash /bin/sh
sudo -u $SUDO_USER "$@"
sudo umount /bin/sh

How can I merge these into a single file? I'm nervous about sticking "@" in a larger string, as it may contain lots of special characters, quotes, backslashes, etc.

Better still, I'd rather be able to provide an entire block, something like

# Pseudo-code
unshare -m start
  # Run arbitrary commands in current shell environment
unshare stop

I'm trying to generalize this solution for temporarily changing /bin/sh as a workaround for a common software bug, eg. in csvtool.

  • If you're root, you can switch back with su to any user you like inside your script. I suggest to also try to generalize my solution to temporarily change /bin/sh to /bin/bash ;-) – mosvy Mar 7 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.