Leon Kasko
Leon Kasko

Reputation: 127

How to create a file containing TAB with cat?

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

Upvotes: 1

Views: 5067

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54563

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.

Upvotes: 1

pmr
pmr

Reputation: 1006

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

Upvotes: 0

Related Questions