Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow arrow keys in read command

Tags:

bash

I made a bash script to use sendmail to send emails. However, when it asks for input, whenever I try to use the arrow keys to correct a typo, instead of moving the cursor, it just adds ^[[D to the end. How can I fix that?

like image 615
NerdOfLinux Avatar asked Aug 15 '17 16:08

NerdOfLinux


People also ask

How do I use the arrow key in CMD?

You can simply press Shift-right-arrow, and then start typing. In contrast, to these combination shift-arrow commands which move the cursor to the beginning and end of lines, the Ctrl-left-arrow and Ctrl-right-arrow keys move the screen image 20 spaces in the opposite direction to the arrow, without moving the cursor.


1 Answers

Use the -e option to read. From the bash manual:

-e
Readline (see Command Line Editing) is used to obtain the line. Readline uses the current (or default, if line editing was not previously active) editing settings.

like image 118
Barmar Avatar answered Sep 26 '22 23:09

Barmar