user8292439
user8292439

Reputation: 1117

How to allow arrow keys in read command

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?

Upvotes: 10

Views: 3302

Answers (2)

Dorian Grv
Dorian Grv

Reputation: 499

If I understood what you want. I searched a long time and did not find example until now so I am not sure this is the best method.

Then you can use arrows to get the history of the shell and what you passt in the past or write in a read cmd.

echo "Give your TV video : " 
read -e -i "" filev # assign read input to $filev, "" is to avoid to write something, do not know why, this is different than read -p "Give your TV video" filev
echo $filev >> ~/.bash_history # write what was written or paste in history
history -a # actualize the sheel history with the ~/.bash_history to get the $filev available in the shell after

Upvotes: 0

Barmar
Barmar

Reputation: 782427

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.

Upvotes: 19

Related Questions