Table of contents

Several ways to output tab characters in shell scripts

Shell Jul 28, 2020 Viewed 304 Comments 0

This article describes several ways to echo the tab character \t in shell scripts, as well as other special symbols, such as new line \n.


-e option

-e - enable interpretation of backslash escapes. For example.

echo -e '\t'


Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

Use $'\t' to output the tab character Tab.

echo Hello$'\t'world.


echo $'hello\tworld'

"" double quotes

echo "[$res]". Double quotes works in zsh, but fails in bash. For example.

echo "hello\tworld"


Use the printf function to format the output of the string.

printf '%s\t%s\n' "$value1" "$value2“
Updated Jul 28, 2020