21

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.

  • 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 Nov 13 '11 at 19:12
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

  • 1
    It 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 Feb 1 '11 at 19:53
  • 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 Feb 1 '11 at 20:19
  • @barlop: What...............? xD – Tamara Wijsman Nov 13 '11 at 12:57
  • 1
    I 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 Jun 30 '16 at 13:28
  • 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 Jul 13 '17 at 21:46
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.

  • 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 Aug 21 '13 at 16:04
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.)

  • Powershell would be very nice, but it's not always availible... – Haas Feb 1 '11 at 19:54
  • @Haas: Please see my edited answer. – Dennis Williamson Feb 1 '11 at 20:51
  • Thanks Dennis. I'd love to use something newer and better, but it's the one option I have for this specific script. – Haas Feb 1 '11 at 21:06
  • @Haas: I just thought that "I'm looking for something more visual" was a requirement. – Dennis Williamson Feb 1 '11 at 21:12
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

  • Ah, powershell is taboo unless someone has already said it. – Pacerier Jul 29 '15 at 4:21
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.


-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

  • 2
    How is it better / different than just pressing tab itself? – Máté Juhász Mar 31 '18 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.