Skip to content

Commit

Permalink
If running under sudo, run brew as the original sudo user (issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Oct 20, 2023
1 parent 4971a1f commit 802f245
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions share/ruby-install/package_manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ function install_packages()
run $sudo pkg install -y "$@" || return $?
;;
brew)
local brew_owner="$(/usr/bin/stat -f %Su "$(command -v brew)")"
local brew_sudo=""

if [[ "$brew_owner" != "$(id -un)" ]]; then
brew_sudo="sudo -u $brew_owner"
if (( UID == 0 )) && [[ -n "$SUDO_USER" ]]; then
# Note: to avoid running homebrew as root,
# drop privileges back to the original sudo user
brew_sudo="sudo -u \"$SUDO_USER\""
fi

run $brew_sudo brew install "$@" ||
Expand Down

0 comments on commit 802f245

Please sign in to comment.