keeplearning
keeplearning

Reputation: 379

how to tab separate variables in unix

I have 2 variable :$table and $i

table=test
i=1

when i try to separate these two variables with tab with the below command:

echo "$table\t$i\tSuccess" > success.txt

The output is

 test\ti

The desired output is

test    1

Appreciate valuable suggestions.

Upvotes: 1

Views: 4431

Answers (2)

Fred
Fred

Reputation: 6995

You can write a tab in Bash as $'\t'.

What I do in my scripts is create a constant. The tab itself is not special: once you have it in a variable, you can use it the normal way.

readonly tab=$'\t'
echo "$table$tab$i${tab}Success" > success.txt

Upvotes: 1

William Pursell
William Pursell

Reputation: 212404

Use printf:

printf "%s\t%s\tSuccess\n" "$table" "$i" > success.txt

or echo -e (the -e flag enables interpretation of the backslash-escaped characters):

 echo -e "$table\t$i\tSuccess" > success.txt

Upvotes: 5

Related Questions