bash - echo backspace 开关创建换行符

标签 bash

我明白了

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

当你在这个命令中把这两个放在一起时:

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

输出:

Enter your age:
_

将 _ 作为您的光标,要求输入。

我的问题:如果我删除\b 开关,光标将在换行符上。有人可以向我解释这是如何工作的吗?

最佳答案

尾随换行符已从 $() 命令替换中移除。引用联机帮助页:

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. (...)

(强调我的)。

在没有 \b 的情况下实现相同行为的一种方法是使用特定于 bash 的 $'' 字符串:

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

或者只是在带引号的字符串中放置一个换行符:

read -p 'Enter your age:
'

格式不太好,但也可以。

关于bash - echo backspace 开关创建换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29676135/

相关文章:

linux - While 循环在 1 次迭代后停止 [BASH]

bash - 如何指定 Bourne shell(不是 sh)

java - 在 BASH 脚本中运行 JAVA 程序时出现问题

linux - 创建一个 bash 别名以输入部分命令

linux - Windows(Linux 子系统)上 Ubuntu 上 Bash 中的 BrowserSync - 无法打开浏览器

linux - Minicom 运行脚本不理解变量

java - 通过 Java ProcessBuilder 激活 virtualenv

macos - 在 OS X 上设置环境变量

linux - 如何跟踪文件中模式的倒数第二个出现

bash - 如果 serverName 为 Null,则删除 Azure SQL 数据库的脚本