[英]How to enter a tab char on command line?
In an interactive bash terminal how do I enter a tab character? 在交互式bash终端中,如何输入制表符? For example, if I wanted to use sed to replace "_" with tabs I'd like to use:
例如,如果我想使用sed将“_”替换为我想使用的标签:
echo $string | sed 's/[_]/TAB/g'
Where TAB means the tab key. 其中TAB表示Tab键。 This works in a shell script not interactively where when I hit the tab key I get no character and a clank noise sounds.
这不是交互式的shell脚本,当我按Tab键时,我没有任何字符和铮铮声。 I've also tried
\\t
but it only places t's in the string and not tabs. 我也试过
\\t
但它只将t放在字符串而不是标签中。
Note this is mac osx. 注意这是mac osx。
用Control + V取代它,然后用Tab取消通常的扩展行为。
Since this question is tagged "bash"... using the "ANSI-C quoting" feature of the Bash shell is probably preferable. 由于这个问题被标记为“bash”...使用Bash shell的“ANSI-C引用”功能可能更可取。 It expands ANSI C-style escape sequences such as
\\t
and \\n
and returns the result as a single-quoted string. 它扩展了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. 这不依赖于您的终端理解Control + V (字面插入下一个字符)键绑定 - 有些可能不会。 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
sed
invocation in a script that other people might end up reading. 此外,因为所有“隐形”字符都可以按字面表示,所以您的解决方案可以在不丢失信息的情况下进行复制粘贴,并且如果您在脚本中使用此
sed
调用,则其他人可能会更加明显/持久最终阅读。
Also note that macOS's version of sed
is the BSD version. 另请注意,macOS的
sed
版本是BSD版本。 GNU sed
will interpret character escapes like \\t
in the replacement pattern just fine, and you wouldn't even need above workaround. GNU
sed
会在替换模式中解释像\\t
这样的字符转义就好了,你甚至不需要上面的解决方法。
You can install GNU sed with MacPorts , and it should be made available as gsed
, but Homebrew might supersede your system's sed
depending on how you have your $PATH
variable arranged. 您可以使用MacPorts安装GNU sed,它应该可以作为
gsed
,但Homebrew可能会取代您的系统的sed
具体取决于您如何安排$PATH
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.