使用制表符支持 (\t) 读取 Linux Bash Shell

Wol*_*olf 3 bash read

\t水平标签选项echo

wolf@linux:~$ echo hello
hello
wolf@linux:~$ 

wolf@linux:~$ echo -e '\thello'
    hello
wolf@linux:~$ 
Run Code Online (Sandbox Code Playgroud)

有没有类似的选项read

wolf@linux:~$ read -p 'hello '
hello wolf
wolf@linux:~$ 
wolf@linux:~$ read -p '\thello '
\thello wolf
wolf@linux:~$ 
Run Code Online (Sandbox Code Playgroud)

期望输出

wolf@linux:~$ read -p '\thello ' <- need something to produce something like tab or `\t` in `echo`
    hello wolf
wolf@linux:~$ 
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 6

您始终可以使用 ksh93 样式$'...'的引号形式来理解这些转义序列:

IFS= read -r -p $'\thello ' var
Run Code Online (Sandbox Code Playgroud)

IFS=-r不相关,我只是在这里添加它们,因为read没有它们的调用很少有意义)。

请注意,这-p不是标准sh功能。在 ksh/zsh 中,-p是从协程中读取,并用 指定提示read 'var?Prompt: '。很遗憾bash选择在这里引入不兼容的 API。您不必使用-p,您可以轻松地执行以下操作:

printf >&2 '\thello '
IFS= read -r var
Run Code Online (Sandbox Code Playgroud)

printf确实在其格式参数和%b说明符参数中识别这些转义序列。是否echo识别它们(或接受一个-e选项来识别它们)取决于实现,并且对于bash构建时间和运行时设置的许多实现(包括内置),因此最好避免