bash 如何在命令行中输入制表符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6392249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:39:56  来源:igfitidea点击:

How to enter a tab char on command line?

macosbash

提问by grok12

In an interactive bash terminal how do I enter a tab character? For example, if I wanted to use sed to replace "_" with tabs I'd like to use:

在交互式 bash 终端中,如何输入制表符?例如,如果我想使用 sed 将“_”替换为我想使用的选项卡:

echo $string | sed 's/[_]/TAB/g'

Where TAB means the tab key. This works in a shell script not interactively where when I hit the tab key I get no character and a clank noise sounds. I've also tried \tbut it only places t's in the string and not tabs.

其中 TAB 表示制表键。这适用于非交互的 shell 脚本,当我按下 Tab 键时,我没有任何字符,并且会发出叮当声。我也试过,\t但它只将 t 放在字符串中而不是制表符中。

Note this is mac osx.

请注意,这是 mac osx。

回答by geekosaur

Precede it with Control+ V, followed by Tabto suppress the usual expansion behavior.

在它前面加上Control+ V,然后是Tab抑制通常的扩展行为。

回答by TheDudeAbides

Since this question is tagged "bash"... using the "ANSI-C quoting" featureof the Bash shell is probably preferable. It expands ANSI C-style escape sequences such as \tand \nand returns the result as a single-quoted string.

由于这个问题被标记为“bash”……使用Bash shell的“ANSI-C 引用”功能可能更可取。它扩展,如ANSI C风格的转义序列\t,并\n返回结果为单引号的字符串。

echo $string | sed $'s/_/\t/g'

This does not rely on your terminal understanding the Control+V(insert next character literally) key binding—some may not. Also, because all the "invisible" characters can be represented literally, your solution can be copy-pasted without loss of information, and will be much more obvious/durable if you're using including this sedinvocation in a script that other people might end up reading.

这不依赖于您的终端理解Control+ V(按字面意思插入下一个字符)键绑定——有些可能不会。此外,由于所有“不可见”字符都可以按字面意思表示,因此您的解决方案可以复制粘贴而不会丢失信息,并且如果您使用sed在其他人可能会结束的脚本中包含此调用,则该解决方案将更加明显/耐用起来阅读。

Also note that macOS's version of sedis the BSD version. GNU sedwill interpret character escapes like \tin the replacement pattern just fine, and you wouldn't even need above workaround.

另请注意,macOS 的版本sed是 BSD 版本。GNUsed会像\t在替换模式中一样解释字符转义,您甚至不需要上述解决方法。

You can install GNU sed with MacPorts, and it should be made available as gsed, but Homebrewmight supersede your system's seddepending on how you have your $PATHvariable arranged.

您可以使用MacPorts安装 GNU sed ,它应该作为 提供gsed,但 Homebrew可能会取代您的系统,sed具体取决于您如何$PATH安排变量。