How do I echo a TAB char on a command prompt

21

1

On a windows batch file, what is the proper way to echo a TAB?

echo A<TAB>B<TAB>C

I know I can type the TAB char between entries, but most editors will display it as a sequence of spaces. Some will even automatically convert them to spaces, I'm looking for something more visual.

Clarification: <TAB> here means the real tab char. I'm looking for the the C \t in a batch script.

Haas

Posted 2011-02-01T16:48:17.853

Reputation: 313

You tried editors and found "most" display tab as a sequence of spaces? which are those? The first editor i'd have tried is notepad and that displays tabs as tabs. – barlop – 2011-11-13T19:12:28.753

Answers

17

Just hit the TAB key in an editor that supports it, Notepad for example.

So, if I enter this:

@echo 1<TAB>b
@echo 2<TAB>c

It would result in this:

enter image description here

Tamara Wijsman

Posted 2011-02-01T16:48:17.853

Reputation: 54 163

1I use the suggestion of Tom Wijsman, but with quoting. SET "TAB= " where the whitespace is actually a TAB (0x09) character. It is clearly suboptimal, but it seems to be the best alternative I have seen. – lit – 2016-06-30T13:28:20.453

To get the TAB character when pressing the TAB key, you must start command prompt like cmd /F:OFF to disable auto-completion of file/dir. names... – aschipfl – 2017-07-13T21:46:38.007

1It works, and it's how I'm doing it. The problem is that anyone editing the file might replace an oddly positioned tab withouth noticing it's intentional. – Haas – 2011-02-01T19:53:52.990

5@Haas: What about set TAB=<TAB> to clarify that it is the tab character and then using %TAB% afterwards? As far as I am aware, echo only parses %...%. There is no built-in tab character as far as I'm aware of... – Tamara Wijsman – 2011-02-01T20:19:44.320

@barlop: What...............? xD – Tamara Wijsman – 2011-11-13T12:57:42.783

11

One solution is that you can set an environment variable called TAB and set the value to the actual tab character. You may need to copy and paste the tab character from a text editor to get it to be entered correctly. I have tried this in Windows 7 and it works.

In your batch file, just use %TAB% and it will insert a tab character.

Timothy C. Quinn

Posted 2011-02-01T16:48:17.853

Reputation: 272

Yip this one is the best as it saves dealing with unknown tabs as there wouldn't be easily spotted otherwise. set tab1="<TAB>" & set tab2="<TAB><TAB>" etc.... – Ste – 2020-01-20T00:43:14.437

3+1 sombody voted you down maybe if this answer can be found somewhere in the comments. But answers should be found in the answers and not in the comments therefore an upvote from me – miracle173 – 2013-08-21T16:04:15.210

3

Limitations such as this are among the reasons to use Windows Script Host or Powershell.

Windows Script Host shipped (ships) with every Windows version from 98 on and can be installed on 95 and NT 4.

Create a file called demo.vbs and paste the following line in it and save it.

WScript.StdOut.WriteLine "a" + chr(9) + "b"

Now, from the directory where you saved it, enter:

demo.vbs

and you should see:

a       b

You can also do

cscript demo.vbs

which will allow you to use the command line switches that cscript provides.

(Tested on Vista.)

Paused until further notice.

Posted 2011-02-01T16:48:17.853

Reputation: 86 075

Powershell would be very nice, but it's not always availible... – Haas – 2011-02-01T19:54:59.793

@Haas: Please see my edited answer. – Paused until further notice. – 2011-02-01T20:51:44.147

Thanks Dennis. I'd love to use something newer and better, but it's the one option I have for this specific script. – Haas – 2011-02-01T21:06:07.623

@Haas: I just thought that "I'm looking for something more visual" was a requirement. – Paused until further notice. – 2011-02-01T21:12:21.430

1

Here is a one line solution using powershell in a batch file:

Powershell -noprofile -nologo -command Write-Output "a`tb`tc"

the `t is the tab character

stangm

Posted 2011-02-01T16:48:17.853

Reputation: 11

Ah, powershell is taboo unless someone has already said it. – Pacerier – 2015-07-29T04:21:25.757

0

As long as you are using an editor that keeps the tabs intact, you could download sed and put it on your path, and then you could do something like:

echo "A`B`C" | sed 's/\t/<TAB>/g'

where each ` is standing in for a real tab.


farfromhome

Posted 2011-02-01T16:48:17.853

Reputation: 537

-1

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. Char which look as bolded O should appear, otherwise some sort of encoding or keyboard mapping is on.

If your Editor was set to plain ASCII encoding, or binary mode, you can save this sequence to file. If you dont have such one, try typying CMD:

ECHO a (sequence LALT+9) b > tabchk.bat tabchk.bat

psiedlaczek

Posted 2011-02-01T16:48:17.853

Reputation: 1

2How is it better / different than just pressing tab itself? – Máté Juhász – 2018-03-31T13:38:52.297