2 add explanation
source | link

If you want it to work without sudo as well, use ${SUDO_USER:-$USER}. For example:

printf '%s\n' "${SUDO_USER:-$USER}"

Explanation

${var:-val} will expand to $var, unless it's unset or empty, in which case it will expand to val.

If you want it to work without sudo as well:

printf '%s\n' "${SUDO_USER:-$USER}"

If you want it to work without sudo as well, use ${SUDO_USER:-$USER}. For example:

printf '%s\n' "${SUDO_USER:-$USER}"

Explanation

${var:-val} will expand to $var, unless it's unset or empty, in which case it will expand to val.

1
source | link

If you want it to work without sudo as well:

printf '%s\n' "${SUDO_USER:-$USER}"