sudo.sh 907 B

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