Skip to content

Instantly share code, notes, and snippets.

@soraxas
Created March 21, 2024 00:28
Show Gist options
  • Save soraxas/7dcc7b1918ac8df99f8e6ac3c9a2edcc to your computer and use it in GitHub Desktop.
Save soraxas/7dcc7b1918ac8df99f8e6ac3c9a2edcc to your computer and use it in GitHub Desktop.
This is a transparent wrapper that pretty much does nothing (aka it passthrough and eval all arguments). This is useful in-place of `sudo`, where you might have sudo-right (e.g. inside a container) but cannot perform `setuid` (because you are not actually sudo or using fakeroot as far as the host-machine's concern).
#!/bin/sh
requote_args() {
# Given a list of args, add quotes if necessary
C=''
for i in "$@"; do
case "$i" in
*[[:space:]]*|*'*'*) # contain space or literal astrisk
case "$i" in
*\'*) # escape single quote
i="$(printf "%s" "$i" | sed "s/'/'\"'\"'/g")"
;;
*) : ;;
esac
# quote only the parts after equal sign
# i.e. in the format of pre='echo foo && echo bar'
case "$i" in
*=*) # escape single quote
i="${i%%=*}='${i#*=}'"
;;
*) # normal case
i="'$i'" ;;
esac ;;
*) # no space, do nothing
: ;;
esac
if [ -z "$C" ]; then
C="$i"
else
C="$C $i"
fi
done
printf "%s" "$C"
}
eval "$(requote_args "$@")"
@soraxas
Copy link
Author

soraxas commented Mar 21, 2024

If this is save as /usr/bin/sudo, it will works with

sudo echo Hi

as well as

sudo FOO=bar echo Hi

where the second one will fail if you don't do eval (and won't works correctly if you don't requote the arguments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment