7

I'm trying to write a script that requests the user password for some sudo-related commands. My goal is to emulate the default method that OS X uses to obtain the user's password, Password: followed by a key-like symbol, as seen in the screenshot attached below:

key symbol

I tried the answer from How to get a password from a shell script without echoing over on Stack Overflow that uses read -s, but that results in Password: without the key symbol when run from a script, but displays the symbol when run from the Terminal itself . What is this key symbol, and can I find it under the Characters menu?

4
  • I get the key symbol when running read -s in Terminal, so I assume this is actually a feature of bash. Maybe a look into the source code (opensource.apple.com/source/bash/bash-99/bash-3.2/builtins/…) might give more clues.
    – nohillside
    May 9, 2016 at 8:44
  • @patrix I see, now I understand. I did not have read -s set up correctly in my script. Thanks for your help! May 9, 2016 at 8:46
  • @patrix Kindly do add an answer if you don't mind. May 9, 2016 at 8:52
  • Check. And it's probably a feature of Terminal even.
    – nohillside
    May 9, 2016 at 8:56

3 Answers 3

8

I get the key symbol when running read -s in Terminal.

As this seems to work the same for read -s as for sudo, I assume this is a feature of Terminal (to display a key symbol whenever input is requested in silent/hidden mode).

2

The simplest answer is just write the script without sudo

then tell the user to run it with sudo e.g. sudo script

the script will run with root privileges

`this has the advantage that the user knows what asks for and where the password is used, if in your script you could be taking a copy or leaving it in memory

2
  • 1
    Thanks for your prompt reply. While I understand that you can run a script in its entirety as a root user, I would like to emulate the password-obtaining mechanism of sudo. I apologise if I was unclear. May 9, 2016 at 8:43
  • 1
    You were clear but I think it is a bad idea
    – mmmmmm
    May 9, 2016 at 8:44
1

I think it has something to do with whether the terminal echo option is disabled. I tried running stty -echo; cat; stty echo in my terminal and then typing something, and the key symbol showed. I then pressed Ctrl+D and it went away.

You must log in to answer this question.

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