简体   繁体   English

echo backspace开关创建换行符

[英]echo backspace switch creates newline

I understand 我明白

\n = new line or carriage return
\b = move cursor to the left once (does not delete anything)

When you put those two together in this command: 将这两者放在此命令中时:

read -p "$(echo -e 'Enter your age: \n\b')"

That outputs: 那输出:

Enter your age:
_

With _ being your cursor asking for an input. 用_是你的光标要求输入。

My Question : If I remove the \\b switch, the cursor will not be on a newline. 我的问题 :如果我删除\\ b开关,光标将不在换行符上。 Can someone explain to me how this works? 有人可以向我解释这是如何工作的吗?

Trailing newlines are removed from $() command substitutions. $()命令替换中删除尾随换行符。 Quoth the manpage: 手册页:

Command substitution allows the output of a command to replace the command name. 命令替换允许输出命令来替换命令名称。 There are two forms: 有两种形式:

  $(command) 

or 要么

  `command` 

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Bash通过执行command并用命令的标准输出替换命令替换来执行扩展, 删除任何尾随换行符。 (...) (......)

(Emphasis mine). (强调我的)。

A way to achieve the same behavior without \\b is to use bash-specific $'' strings: 在没有\\b情况下实现相同行为的方法是使用特定于bash的$''字符串:

read -p $'Enter your age:\n'

Or to just put a newline inside a quoted string: 或者只是在引用的字符串中添加换行符:

read -p 'Enter your age:
'

The formatting isn't pretty, but it'll also work. 格式不是很好,但它也可以工作。

(Just for information when I run your \\b version my cursor is on the end of the first line not at the start of the second line but that's likely just a terminal difference.) (仅当我运行你的\\b版本时,我的光标位于第一行的末尾,而不是在第二行的开头,但这可能只是一个终端差异。)

By way of illustration compare this: 举例来说,比较一下:

$ printf "$(echo -e 'Enter your age: \n ')_" | xxd
0000000: 456e 7465 7220 796f 7572 2061 6765 3a20  Enter your age:
0000010: 0a20 5f                                  . _

and this: 还有这个:

$ printf "$(echo -e 'Enter your age: \n\b')_" | xxd
0000000: 456e 7465 7220 796f 7572 2061 6765 3a20  Enter your age:
0000010: 0a08 5f                                  .._

To this: 对此:

$ printf "$(echo -e 'Enter your age: \n')_" | xxd
0000000: 456e 7465 7220 796f 7572 2061 6765 3a20  Enter your age:
0000010: 5f                                       _

Notice the 0a20 in the first output? 注意第一个输出中的0a20 That's the newline and space from our pattern. 这是我们模式的新线和空间。

Notice the 0a08 in the second output? 注意第二个输出中的0a08 That's the newline and backspace from our pattern. 这是我们模式中的换行符和退格键。

Now look at the third output. 现在看第三个输出。 We don't have any character after the newline so we don't see 20 or 08 in that output which makes sense but where did the newline go? 换行后我们没有任何字符,所以我们在输出中没有看到2008哪个有意义但换行在哪里去了?

Turns out this is a "feature" of Command Substitution . 事实证明这是命令替换的“特征”。

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Bash通过执行命令并用命令的标准输出替换命令替换来执行扩展,删除任何尾随换行符。

So by putting \\b after the newline you are protecting it from being deleted by the command substitution. 因此,通过在换行符之后放置\\b 您可以保护它不被命令替换删除。

Wintermute covers the correct workarounds/solutions in his answer (you could also put the string in a variable and expand it in quotes on the read line to get the formatting on that line a little nicer). Wintermute在他的答案中涵盖了正确的解决方法/解决方案(你也可以将字符串放在变量中并在read行的引号中展开它以使该行上的格式更好一些)。

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

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