#!/usr/bin/env bash set -o errexit set -o pipefail set -o nounset # Source: https://askubuntu.com/a/937596 sudo() { [[ "${EUID}" == 0 ]] || set -- command sudo "${@}" # If you're the root user then short circuit out of this OR condition. This # is the Docker use case. # # $EUID is the effective user. Unlike $USER, $EUID should be defined in most # Docker images. I've seen it available in every Debian Slim based image. # # Otherwise, take whatever arguments you passed into this function and then # run it through the sudo command. This is the personal machine use case and # is the other side of the OR condition. # # `command` is a bash command to execute or display info about a command and # we're using `set` to modify the args of ${@}. "${@}" # Run whatever arguments you passed into this function. This would be whatever # the command was but without `sudo`. }