How to create a file containing TAB with cat?

4.7k views Asked by At

I wanted to create a file in my bash script using cat containing a TAB Char. However I couldn't manage to get it working. Here my tests:

Using plain tab:

cat >file.txt <<END
    Text after plain tab.
END

Result:

Text after plain tab.

Using ^I:

cat >file.txt <<END
^IText after escaped I.
END

Result:

^IText after escaped I.

Using \033I:

cat >file.txt <<END
\033IText after another escaped I.
END

Result:

\033IText after another escaped I.

Thanks in advance :-)

2

There are 2 answers

1
Thomas Dickey On BEST ANSWER

There are three examples shown. The first works (perhaps some detail was omitted). The other two (a literal "^I" and a literal "\033I") will not produce a tab because cat does not do that. It does provide an option (-v) for going the other way, making the nonprinting characters displayable.

As noted in another answer, the echo command can interpret some backslash sequences. For example

echo "\tExample with tab" >file.txt

There are some differences between the varieties of echo used on Linux as a standalone program (from coreutils) versus that which is part of the shell (perhaps bash or dash or zsh). The POSIX standard is a good place to start.

0
pmr On

You can easily use Echo and tab character \t as shown below:

[purnendu.maity@unix ~]$ echo  "Example with tab: \t text after tab" > file.txt
[purnendu.maity@unix ~]$ cat file.txt
Example with tab:        text after tab