Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: When reading user input, Enter brings new line

Tags:

bash

I need following sample bash script to behave as follows:

echo -e "Enter name: \c"
read U_IP_NAME
echo -e "You said your name is : $U_IP_NAME"

This will output to:

Enter name: Alok
You said your name is : Alok

But it I want it to be:

You said your name is : Alok

Is there a way to achieve this?

[Solved with solution given by: mouviciel]

like image 520
That krazee guy Avatar asked May 10 '12 14:05

That krazee guy


2 Answers

You want to move the cursor up one line. This is achieved with tput cuu1:

echo -e "Enter name: \c"
read U_IP_NAME

tput cuu1

echo -e "Your said your name is : $U_IP_NAME"

More info with man tput and man terminfo.

like image 111
mouviciel Avatar answered Oct 21 '22 07:10

mouviciel


read -p "Enter your uip-name: " U_IP_NAME

-p for prompt

like image 26
user unknown Avatar answered Oct 21 '22 06:10

user unknown