Get the backspace key input from the user in bash

Is there a way I can get the backspace key input from the user in a bash script ?
I tried the following but none of them worked:

read -n2 input

if [ "$input" = '\b' ]
then
echo 'you typed the backspace key'
else
echo 'you typed another key'
fi

besides \b I also tried ^?, \010 without success.
Can it be done ? I would also like to map other different keys like clear ( num lock ) and return.

Thanks in advance
Pedro

Mac OS X (10.4.5)

Posted on Mar 21, 2006 12:36 PM

Reply
11 replies

Mar 21, 2006 5:50 PM in response to Pedro Estarque

Hi Pedro,
What do you mean when you say "backspace"? There is no such character on an Apple keyboard. Do you mean ASCII character 8 or ASCII character 127?

Apple keyboards have a "delete" key where PC keyboards have a "backspace" key. Also, the character sent to an application when you hit the "backspace" key on a PC is inconsistent. Some operating systems send ASCII character 8, which is traditionally referred to as the backspace character. There is a shell representation of this character, ^H. Others send ASCII character 127, for which the shell representation is ^?. This is the character sent by Apple's "delete" key, as has been the case since the beginning, when there was no "Windows" OS.

You are correct that "\b" is two characters but no keyboard sends those characters to an app when a single key is depressed. When a string in the bash shell is enclosed in single quote preceded by a dollar sign, $' ', such character sequences are interpreted as "backslash-escaped characters" and are expanded into the characters they represent. You are correct that \b is the backslash-escaped character sequence for the classic backspace character, ASCII character 8, but your code is missing the dollar sign necessary to cause these sequences to be expanded. Thus the correct conditional in your code should be:

if [ "$input" = $'\b' ]

However, that is a single character so you need to change your "read" line to grab just one character, like so:

read -n1 input

Also, no key on Apple's keyboard produces such a character. A method for producing this character in modern terminals is the following key sequence:

<Control>-v, <Control>-h

That is, while holding down the control key, hit the v and h keys, in that order.

If, on the other hand, you are actually trying to grab Apple's "delete" key, the following conditional will do that:

if [ "$input" = $'\x7f' ]

I hope that helps.
--
Gary
~~~~
ASCII and ye shall receive.

Mar 22, 2006 6:47 AM in response to Gary Kerbaugh

Thanks a lot Gary!!!

yeah, I meant ASCII 8. I just said backspace to differentiate it from the del key, I thought many people here at the Unix forum would have a PC background.

I had also tried
if [ "$input" = '^H' ] ( shift-6-shift-h )
but that obviously didn't work.

Now how did you know it was 'x7f' ? That's the hexadecimal representation of it right ?
I use a small app ( ASCII Table ) that show the respective values for each key, and it shows 0x08 for delete, and 0x7F for del.
The values I get are: decimal = 8, hex = 0x08, octal = 010
So in my understanding either:
$'\x008' $'\x08' $'\x8'
or
$'\010'
should work, but it doesn't

Are this software values wrong? How do you know what value represents each key ?

Thanks a lot again

Mar 22, 2006 10:59 AM in response to Pedro Estarque

How do you know what value represents each key ?


echo -n '<ctrl-v><TheKey>' | od -x

Here, <ctrl-v> is to hit the v-key while holding down the control key, and <TheKey> is to hit the key whose value you want to know. If TheKey is the delete key, then '<ctrl-V><TheKey>' means hitting the following four keys:
<pre>
' ctrl-v delete '
</pre>
The output will look like
% echo -n '^H' | od -x
0000000 0800
0000001

08 is the hex value for the key; 00 is just a padding.

Is this what you want to know?

PowerMac G4 Mac OS X (10.4.5)

Mar 22, 2006 2:36 PM in response to Pedro Estarque

Hi Pedro,

> I just discovered that \177 works too.
So this apps values must be wrong.


You didn't read my post carefully enough; that's one of the things I was trying to tell you. Also, you haven't been providing accurate information on what you want to do, which is why I had to provide information about two characters.

You keep saying that you want to trap the backspace character but what you really want is to trap the character that's produce when you hit "that wide key at the top right of the main part of the keyboard". On an Apple keyboard that key has "delete" written on it. I guess that you must have a PC keyboard because they have "backspace" written on that key. However, that doesn't matter; on an Apple computer, no matter what is written on the key, that wide key sends a DEL character, ASCII 127. (thanks Andy, I forgot the name) That's 127 in base 10. In base 8, or octal, that's 177 and in base 16, hex, that's 7F.

On the other hand as I said before, you can make any of these keyboards send the backspace character, although not with a single keystroke. To send a backspace character in a terminal, use the key sequence:

<Control>-v, <Control>-h

In the terminal you will see the characters ^H. Just remember that that's merely a human readable representation. There's only one real character produced, the backspace. There's no actual carat or H characters involved.
--
Gary
~~~~
Why a man would want a wife is a big mystery to
some people. Why a man would want two wives is
a bigamystery.

Mar 22, 2006 7:54 PM in response to Nils C. Anderson

Well I was scratching my head, about why the script worked before I posted it and why it didn't afterwards. Then I remembered that I was in process of reloading the OS, and that I had temporarily set the TERM to Apple_Terminal. Which has the correct mapping for the "delete" key.

infocmp Apple_Terminal | grep kbs
kbs=\177

where as the default term, has it set to:

infocmp xterm-color | grep kbs
kbs=^H

and since as Gary and Jun T have pointed out, the delete key returns \177
and not '^H' the script wasn't finding the value it was expecting when the term was set to xterm-color.

Anyway it's a moot point now.

Andy

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Get the backspace key input from the user in bash

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.