0

I currently use the command-line utility security in a shell script to access a password from keychain. I am surprised that echoing my password variable works and reveals the password in my terminal window.

I used the function get_pw suggested here and then simply assign the password to a variable PASSWORD. Is it possible to make terminal hide the password?

My shell script looks as follows:

ACCOUNT_NAME='MyAccountName'

get_pw() {
   security 2>&1 >/dev/null find-generic-password -ga $ACCOUNT_NAME \
   | ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}

PASSWORD=$(get_pw)

Then when writing echo $PASSWORD, the password is revealed.

3
  • Please add some details. Do you run PASSWORD=$(get_pw ...) and see the password also in Terminal? If yes, please add your definition of get_pw to the question. If no, please add the commands you use.
    – nohillside
    Sep 22, 2019 at 14:08
  • Thanks for your input. I added it to the question. Sep 22, 2019 at 14:16
  • See below, but I'm not sure what you expect to happen instead.
    – nohillside
    Sep 22, 2019 at 14:23

1 Answer 1

1

Well,

PASSWORD=$(get_pw)

assigns the password to PASSWORD so it shouldn't be a surprise that

echo "$PASSWORD"

actually prints it on the Terminal.

The idea here is not to print it but to use it in another command to pass it on (as the example in the page you've linked to does). Even then the password is still visible in ps -E though.

4
  • Thank you for your answer. Maybe I'm missing something but doesn't that mean that when I'd like keychain to reveal my password, it prompts for my admin pw. But if I try using security, it doesn't? Sep 22, 2019 at 15:17
  • @PatrickBalada When I tried myself, I got prompted for my password to unlock the keychain. There is an "Allow always" option in the prompt, maybe you clicked this a while ago.
    – nohillside
    Sep 22, 2019 at 15:24
  • That's it. When I click it, terminal starts hiding the password. If you add this somehow to your answer, I'd love to accept it. Sep 22, 2019 at 15:54
  • 2
    @PatrickBalada I'm not sure I understand what you are doing? Bash has no way of knowing that the string within PASSWORD is a password from Keychain, how does it turn hidden?
    – nohillside
    Sep 22, 2019 at 16:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .