Shell – How to find what keys the “erase” and “line-kill” characters are in Ubuntu

command linehistorykey mappingshelltty

I'm new to Unix and have bought today a copy of "The Unix Programming Environment".
I'm trying out the stuff from the book. But some of them are not working as expected like :
To kill a line and re-type it again, @ character should be used :

book

$ ddtae@
date
Thu Nov 28 18:12:47 IST 2013

my terminal

$ ddtae@
ddtae@: command not found

Another example is to use # to erase last character

book

$ dd#att#e#e

which comes out as date and print it.

my terminal

$ dd#att#e#e
dd#att#e#e: command not found

in my system # is used for commment

Although they have mentioned that these characters are system dependent.
How can i find the characters for my system to perform above two tasks.

Best Answer

Terminal line control can be queried and/or set by stty. To see the current settings, use stty -a. The manpages provide details.

For example, from stty -a you might find this kill-line control:

kill = ^U

The caret means hold the control key (Ctrl) and then type the character shown (U). To change the line-kill sequence, you could do:

$ stty kill \@

NOTE: The backslash is an escape to signify that the character following is to be interpreted literally by the shell.

Having changed your line-kill to this, (a literal @), you can now obliterate a line that looks like:

$ ddtae@

NOTE: In the above, scenario, when you type ddtae, when you type the character @, the entire line will be erased.

One way to restore the default settings (and this is very useful when you have inadvertently altered settings) is to simply do:

$ stty sane

Yet another use of stty is to control character echo-back. For instance, a simple way to hide a user's password as (s)he types it is to do:

#!/bin/sh
echo "Enter password"
stty -echo
read PWORD
stty  echo
echo "You entered '${PWORD}'"
Related Question