Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo tab characters in bash script

How do I echo one or more tab characters using a bash script? When I run this code

res='       'x # res = "\t\tx" echo '['$res']' # expect [\t\tx] 

I get this

res=[ x] # that is [<space>x] 
like image 229
kalyanji Avatar asked Feb 08 '09 15:02

kalyanji


People also ask

How do you echo a tab character?

Use the verbatim keystroke, ^V ( CTRL+V , C-v , whatever). When you type ^V into the terminal (or in most Unix editors), the following character is taken verbatim. You can use this to type a literal tab character inside a string you are echoing.

How do I add a tab in bash?

Ctrl-v, Ctrl-i will also work (I found this answer here). Also, I think a Ctrl-q, Ctrl-v, Tab will work. Thanks L2G!

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


1 Answers

echo -e ' \t ' 

will echo 'space tab space newline' (-e means 'enable interpretation of backslash escapes'):

$ echo -e ' \t ' | hexdump -C 00000000  20 09 20 0a                                       | . .| 
like image 188
Johannes Weiss Avatar answered Sep 27 '22 23:09

Johannes Weiss