Home > Shells > Turn off echo in a terminal

Turn off echo in a terminal

Imagine you are writing a shell script that requires the user to input confidential information, lets say a password.

The user wont feel comfortable if the password is echoed on the screen like a simple text. If you ever used expect interactive scripting, you know for sure this kind of problem.

Don’t worry, its amazingly easy to perform this trick and stop echoing whatever the user types on the screen.

Simply add the following line to your script:

stty -echo

and you are done. The stty output will go offline. Let’s see an example:

#!/bin/bash
echo Hello

stty -echo
# do what ever you want to do
echo 'I slept with your girlfriend'
stty echo
echo Bye
exit 0;

That’s it! Just remember to put it back to normal with the following command:

stty echo

Also, it would be helpful for you to read the whole man page for stty.

$ man stty

Advertisements
Categories: Shells Tags:
  1. soham
    June 12, 2012 at 10:09

    What if I want to stop giving out error messages for a while inside a shell script. ?

  2. September 12, 2013 at 11:26

    how about the reverse ? whats the command for that ?

  3. MetaL
    February 18, 2014 at 02:11

    “muthuveera”: please explain what would be the “reverse” state that you asked. I think once you explain what you want, the answer will be clear to you.

  4. February 18, 2014 at 02:30

    The reverse implies running “stty echo” (without the minus sign). Or reset the terminal by running “reset” (even though you may not be able to see what you’re typing).

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: