简体   繁体   中英

How to enter a tab char on command line?

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:

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 \\t but it only places t's in the string and not tabs.

Note this is mac osx.

Control + V取代它,然后用Tab取消通常的扩展行为。

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

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 sed invocation in a script that other people might end up reading.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM