Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use stty to facilitate interactive script input UNIX
I noticed that under some circumstances, such as using "gets" in tcl, or "read" in certain shell scripts, that when I made a typo and pressed the "delete" key to back up, it just echoed ^H at me and didn't erase the mistake. After some fiddling around, I discovered that if I ran the command
    stty erase ^H
things started working correctly. I guess the erase character wasn't getting set automatically, but the shells were reading input in raw mode and doing their own editing so I hadn't ever realized it. (Or maybe there's some other explanation???). To type ^H, what I do is to type Control-V Control-H. It's possible that you may need to do something else such asL
\ Control-H
or the like, depending on various settings in your system. What you're trying to do is to escape the normal meaning the CTL-H key has to your shell. To make it more automatic, I added the command to /etc/profile. If you want to put this into a script and then put things back the way they were before exiting, you can use stty -g to get the previous settings in a ready-to-use format (see man stty for more details).
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[7,708 views]  

Use stty to facilitate interactive script input | 3 comments | Create New Account
Click here to return to the 'Use stty to facilitate interactive script input' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Terminal Window Settings...
Authored by: rhowell on Jun 09, '04 11:20:24AM

Will selecting "Delete key send backspace" in Terminal->Window Settings->Keyboard accomplish the same thing?



[ Reply to This | # ]
Terminal Window Settings...
Authored by: owsla on Jun 10, '04 12:01:01PM

No, setting that property doesn't do the same thing as this. I have it set on my terminals, but sftp, for one, still didn't respond properly to the backspace key. With this hint, it does.



[ Reply to This | # ]
Use stty to facilitate interactive script input
Authored by: bluehz on Jun 09, '04 01:02:30PM
Here is a simple example for further explanation. Normally a read statement in a script will wait for input and then a RETURN or ENTER key, but it will not let you backspace over chars if you make a mistake in tying. This simple little line in your scripts make them much more professional allowing the user to edit what they have typed in.
#!/bin/sh

# allow terminal to accept backspace key in read statement
stty erase '^h'

echo -e "Try it out... enter some text and backspace over it... \c"
read test
you can also use the -e switch of the "read" command to enable the readline routines which gives you backspace and arrow keys among other things.
#!/bin/sh

echo -e "Try it out... enter some text and backspace or use arrow keys over it... \c"
read -e test


[ Reply to This | # ]