Table of contents

Several ways to output tab characters in shell scripts

Shell Jul 28, 2020 Viewed 11 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.

echo

-e option

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

echo -e '\t'

$'string'

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.

or

echo $'hello\tworld'

"" double quotes

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

echo "hello\tworld"

printf

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

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