That krazee guy
That krazee guy

Reputation: 103

BASH: When reading user input, Enter brings new line

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]

Upvotes: 10

Views: 9815

Answers (3)

gpojd
gpojd

Reputation: 23065

You can try this syntax:

U_IP_NAME="${U_IP_NAME%\\n}"

Upvotes: 0

user unknown
user unknown

Reputation: 36229

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

-p for prompt

Upvotes: 8

mouviciel
mouviciel

Reputation: 67839

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.

Upvotes: 12

Related Questions