BASH:读取用户输入时,Enter 带新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10536089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:14:55  来源:igfitidea点击:

BASH: When reading user input, Enter brings new line

bash

提问by That krazee guy

I need following sample bash script to behave as follows:

我需要以下示例 bash 脚本的行为如下:

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]

[已解决:mouviciel 给出的解决方案]

回答by mouviciel

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

您想将光标向上移动一行。这是通过以下方式实现的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 tputand man terminfo.

有更多信息man tputman terminfo

回答by user unknown

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

-p for prompt

-p 提示

回答by gpojd

You can try this syntax:

你可以试试这个语法

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

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