Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-30-2011, 06:36 AM   #16
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled

This seems like a lot of work for the objective. What about this:

unset PWORD

echo -n 'password: ' 1>&2

while read -n1 -s char; do

  case "$( echo -n "$char" | od -An -tx1 )" in


    ' 7f')
      if [ -n "$PWORD" ]; then
        PWORD="$( echo "$PWORD" | sed 's/.$//' )"
        echo -n $'\b'' '$'\b' 1>&2

      echo -n '*' 1>&2



echo $PWORD
Kevin Barry
1 members found this post helpful.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-02-2011, 11:22 PM   #17
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 157

Rep: Reputation: 44
The last script is not too bad.

You need to include the IFS= or the password read will abort on space or tab. Also add a -r option, otherwise a backspace at the end will escape the final newline. (A real painful bash default).

I would also include ' 08' (backspace) for character removal and ' 15' (^U or kill line)
to restart the password. But otherwise it is quite good.

  ' 15') # ^U or kill line
        echo -n "$PWORD" | sed 's/./\cH \cH/g' >&2
NOTE that sed assumes GNU-sed.
If this could be a problem another alternative is
      echo -n "$PWORD" | tr -c '\b' '\b' >&2
      echo -n "$PWORD" | tr -c ' '  ' '  >&2
      echo -n "$PWORD" | tr -c '\b' '\b' >&2
All it needs now is for it to not do prompt and star processing when the input is not a TTY! (just a straight pass-thru) and you have a simple (no-gui, no-curses) password reader.

Note if you know the input is from a TTY and wanted a "curses" version try "dialog"
   dialog --stdout --insecure --passwordbox  "Password:" 0 0
The --stdout causes the password out stdout and curse output to go direct to the TTY

This method like most GUI interfaces will not let you input TAB's.

Last edited by A.Thyssen; 10-04-2011 at 10:42 PM.
Old 01-18-2018, 12:02 AM   #18
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 157

Rep: Reputation: 44
Continuing this discussion...

Related to this is the final script as a TTY password input helper...

I have since discovering the linux systemd command "systemd-ask-password"
this is almost perfect for this sort of things, though was not designed for TTY input but for use during computer startup (read password for encrypted disk drive mounting, or computer unlocking).

I also took this problem further, and looks at the temporary caching of passwords for use in things like editing encrypted files, when the same password used to decrypt the file, is later used to re-encrypt the file, and prevent possible mishaps of mistyping the password twice during the re-encryption process.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird characters get typed in terminal sumeet inani Linux - Newbie 8 03-11-2010 10:06 PM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 11:56 AM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
bash script to accept input ONLY until background process completes andrewstr Linux - Software 2 03-17-2004 12:02 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration