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.
Basic way, but not well known, is to send TAB to CMD by using plain ASCII chr(9). This special char can be invoke by pressing 9 on numerical keyboard while holding Left-ALT in the same time.
Press and hold your option key, then type 0009 (zeroes). This is the Unicode sequence for the tab control character.
[Control] + [V] , followed by [Tab] . works on Linux too.
Using the -e flag with echo in a bash script will allow you to output tabs. This was the output after compiled # Add Dyllen's ip for Sourcebox 192.168. 53.215echo -e dev. engage.
Precede it with Control + V, followed by Tab to suppress the usual expansion behavior.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With