No Prompt & Password Fill Install for Homebrew

bashhomebrewpasswordscriptterminal

I'm trying to create a script to deploy macOS and the last piece that isn't automated is installing Homebrew. Because of the unique scenario where Homebrew shouldn't be installed with sudo, and it requires both the return key to be pressed and the users password, it's become a tricky thing to figure out. I've spent a few hours on this and found other unanswered questions on StackExchange with no luck on getting it completely automated.

My code:

echo "Password: "
read -s PASSWORD
echo $PASSWORD | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The echo skips the request to hit return but doesn't pass in the password because I'm missing the -S flag because I shouldn't include sudo when installing Homebrew. I ultimately need a setup that hits command AND passes in the password without using sudo. The rest of my script runs by using echo $PASSWORD | sudo -S my-command-here... so I'd like to not deviate too far from that scheme if possible.

Best Answer

The -s option is working for me, so I don't know why it doesn't work for you.

Have you tried to add #!/bin/sh (or #!/bin/bash) in the top of your script ??

If you use the -n option on echo you'll get the input on the same line as the label.

So the below is having the input: Password: <waiting for input>, and on the next line it just print the password entered - the awk is just for testing that the pipe was working. And then I've added a loop so that something always need to be entered.

#!/bin/sh

readPassword
echo $PASSWORD | awk '{print $1}'

readPassword() {
   echo -n "Password: "
   read -s PASSWORD
   echo ""
   if [[ -z "$PASSWORD" ]]; then
      printf '%s\n' "A password is required..."
      readPassword
   fi
}