我明白了
\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/