简体   繁体   English

bash命令行无法正确读取命令

[英]bash command line can not read command properly

I have a problem about bash terminal, and it made me crazy, and i don't know why. 我对bash终端有问题,这让我发疯了,我不知道为什么。
It is about the terminal, let me describe it as below: 关于终端,让我对其进行如下描述:
At first the prompt is like: 首先,提示如下:

[12:00]ruan:~>  

But when i input characters consecutively (for example, assume i input a lot of A), the weird thing happened: 但是当我连续输入字符时(例如,假设我输入了很多A),就发生了奇怪的事情:

AAA:00]ruan:~ > AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA  

As you can see, character i input starts from the beginning of the line , which made my command unreadable. 如您所见,我输入的字符从该行开头开始 ,这使我的命令不可读。 I don't know how can this happen. 我不知道怎么会这样。 Is it related to stty, inputrc or something else? 它与stty,inputrc或其他相关吗?
/br / BR
ruan

my tty config is like: 我的tty配置是这样的:

:)[11:38]ruan:~ > stty -a
speed 38400 baud; 25 rows; 80 columns;
lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
    -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
    -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
    -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
    -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
    eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
    min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
    stop = ^S; susp = ^Z; time = 0; werase = ^W;

i have setup PS1 like: 我已经安装了PS1,例如:

NORMAL="\033[0m"
RED="\033[0;31m"
RED_BOLD="\033[1;31m"
GREEN="\033[0;32m"
GREEN_BOLD="\033[1;32m"
YELLOW="\033[0;33m"
YELLOW_BOLD="\033[1;33m"
BLUE="\033[0;34m"
BLUE_BOLD="\033[1;34m"
MAGENTA="\033[0;35m"
MAGENTA_BOLD="\033[1;35m"
CYAN="\033[0;36m"
CYAN_BOLD="\033[1;36m"

function smile_or_frown() {
    [[ $? = 0 ]] && echo -e "${CYAN_BOLD}:)" || echo -e "${MAGENTA_BOLD}:("
}
export PS1="\$(smile_or_frown)$BLUE_BOLD[\A]$GREEN_BOLD\u:$RED_BOLD\w$YELLOW_BOLD\$(parse_git_branch)$NORMAL > "

You have to enclose all ANSI escape sequences in \\[ .. \\] to signal to Bash that these don't take up space on the terminal. 您必须将所有ANSI转义序列括在\\[ .. \\]以向Bash发出信号,即它们不占用终端空间。

They have to be in the format string itself, and can not be part of data expanded at prompt time (but can be in data expanded at assignment time). 它们必须是格式字符串本身,并且不能是在迅速时扩展的数据的一部分(但可以是在分配时扩展的数据)。

For example: 例如:

export PS1="\[$BLUE_BOLD\][\A]\[$GREEN_BOLD\]\u:\[$RED_BOLD\]\w\[$YELLOW_BOLD\]\$(parse_git_branch)\[$NORMAL\] > "

To make this work for smile_or_frown, you have to refactor it into two functions, one for the color and one for the text, so that you can do "\\[\\$(smile_color)\\]\\$(smile_type)" 要使它适用于smile_or_frown,您必须将其重构为两个函数,一个用于颜色,一个用于文本,以便您可以执行"\\[\\$(smile_color)\\]\\$(smile_type)"

Run this command to check current terminal columns (width) 运行此命令以检查当前端子列(宽度)

tput cols

And use this to set it to 80 cols: 并将其设置为80 cols:

stty cols 80

EDIT: Based on your edited question it appears your complex PS1 is causing this. 编辑:根据您编辑的问题,看来您的复杂PS1引起了这一问题。 You can reset your PS1 to a simple: 您可以将PS1重置为简单的方法:

PS1='$>'

too fix this issue. 也解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM