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

时间:2021-04-07 20:45:18

I have a problem about bash terminal, and it made me crazy, and i don't know why.
It is about the terminal, let me describe it as below:
At first the prompt is like:

我有关于bash终端的问题,它让我发疯,我不知道为什么。关于终端,让我形容如下:首先提示如下:

[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?
/br
ruan

正如您所看到的,字符i输入从行的开头开始,这使得我的命令不可读。我不知道怎么会发生这种情况。它与stty,inputrc或其他东西有关吗? / 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 > "

2 个解决方案

#1


1  

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)”

#2


0  

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. You can reset your PS1 to a simple:

编辑:根据您编辑的问题,您的复杂PS1会出现这种情况。您可以将PS1重置为简单:

PS1='$>'

too fix this issue.

也解决了这个问题。

#1


1  

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)”

#2


0  

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. You can reset your PS1 to a simple:

编辑:根据您编辑的问题,您的复杂PS1会出现这种情况。您可以将PS1重置为简单:

PS1='$>'

too fix this issue.

也解决了这个问题。