Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Last active February 12, 2026 04:44
Show Gist options
  • Select an option

  • Save tomdavidson/72e539a4fced881a9e6c7b2d9abf5c4a to your computer and use it in GitHub Desktop.

Select an option

Save tomdavidson/72e539a4fced881a9e6c7b2d9abf5c4a to your computer and use it in GitHub Desktop.
snippit to help with scripts that should be run as root
#!/usr/bin/env bash
if [ $SUDO_USER ]; then
realUser=$SUDO_USER
else
realUser=$(whoami)
fi
realUserHome=$(bash -c "cd ~$(printf %q "$realUser") && pwd")
[ "$(id -u)" == 0 ] || exec sudo -E bash "$0" "$@"
! type sudo > /dev/null 2>&1 && apt install sudo && usermod -aG sudo "$(whoami)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment