29

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.

1
  • 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, 2011 at 19:12

7 Answers 7

24

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

5
  • 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, 2011 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... Feb 1, 2011 at 20:19
  • @barlop: What...............? xD Nov 13, 2011 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, 2016 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, 2017 at 21:46
12

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.

2
  • 4
    +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, 2013 at 16:04
  • 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
    Jan 20, 2020 at 0:43
2

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.)

4
  • Powershell would be very nice, but it's not always availible...
    – Haas
    Feb 1, 2011 at 19:54
  • @Haas: Please see my edited answer. Feb 1, 2011 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, 2011 at 21:06
  • @Haas: I just thought that "I'm looking for something more visual" was a requirement. Feb 1, 2011 at 21:12
2

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

1
  • Ah, powershell is taboo unless someone has already said it.
    – Pacerier
    Jul 29, 2015 at 4:21
1

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.


0

echo a TAB char on a command prompt:

set "string=echo A<TAB>B<TAB>C"

set "string=%string:<TAB>=0x09%"
type nul > tmp
forfiles /m "tmp" /c "cmd /c %string%"

Tested on Win 10 cmd

-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

1
  • 2
    How is it better / different than just pressing tab itself? Mar 31, 2018 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.