Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enter a tab char on command line?

Tags:

bash

macos

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.

like image 970
grok12 Avatar asked Jun 17 '11 21:06

grok12


People also ask

How do you type a tab in CMD?

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.

How do you type a tab character?

Press and hold your option key, then type 0009 (zeroes). This is the Unicode sequence for the tab control character.

How do I press tab in terminal?

[Control] + [V] , followed by [Tab] . works on Linux too.

How do you put a tab in a shell script?

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.


2 Answers

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

like image 57
geekosaur Avatar answered Sep 28 '22 04:09

geekosaur


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.

like image 41
TheDudeAbides Avatar answered Sep 28 '22 04:09

TheDudeAbides