(?) The Answer Gang (!)


By Jim Dennis, Ben Okopnik, Dan Wilder, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to linux-questions-only@ssc.com


(?) Readline Features in the Bash 'read' built-in

From Harv Frost

Answered By Jim Dennis

Dear Mr. Dennis,

I've got a problem with cursor keys in a bash shell script and I sure hope you can help me. In it's simplest form the script goes like this:

#!/bin/bash
tput cup 4 47	# position the cursor in a field for input
read afield

If the user makes a mistake typing in this field and tries to backup using the left arrow key, a "^[[D" is displayed instead of the cursor moving backward a character. I've tried working with the inputrc file but no joy.

(!) [JimD] Try read -e to enable the read command to use the readline "editing" features. That might help. However, they'd have to use the [Backspace] or [Ctrl]-[H] key to erase characters (according to your stty settings)

(?) Why is it that the left and right arrow keys work fine on the command line but not from a shell script that reads input? Boy if you can be of help, I'd be forever in your debt. Thanks a million. I've enjoyed reading some of your articles and appreciate your help on so many things.

(!) [JimD] It's documented in the info pages under the section on the read command in the "built-ins" references.
If you don't use read -e then none of the "readline" library routines are active; thus arrow keys won't work.
BTW: I don't recommand that you serious try to build forms applications under bash. You could use the Linux "dialog" utility or Python or Perl curses modules to get much better control over the keyboard and screen then will ever be possible with tput and read commands. I doubt that it is feasible to create a robust full screen interface using sh and commonly available command line utilities like tput, stty, and echo.


This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 64 of Linux Gazette March 2001
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


[ Answer Guy Current Index ] greetings   1   2   3   4   6   7   9 [ Index of Past Answers ]
11   12   15   16   18
20   22   24   25   26   28   29