LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Tab in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/tab-in-bash-script-242400/)

Bobb3 10-13-2004 10:24 PM

Tab in bash script
 
How would i echo a tab? ive tried /t like in c++ and /TAB which i got from google somewhere. Neither worked unless im putting them in the wrong side of quotations.

this is what im trying to do:

touch $Namepath/$1 #create file
echo "\$TTL 3h" >> $Namepath/$1
echo "$1 IN SOA ns1.$1 coldbeaver.$1." >> $Namepath/$1


now i want to echo with a tab so ive tried:

echo "\t blah blah" >> $Namepath

and ive tried

echo /t "blah blah" >> $Namepath

but neither worked. Any help is appreciated

homey 10-13-2004 10:47 PM

You need to use -e to enable the use of special characters. man echo

For example....
echo -e "test \t\t test"

przemek.klosowski 06-15-2011 01:09 PM

'echo -e' works because of built-in functionality of echo. In general, if you want to insert tab in bash, you can force a literal tab by typing Ctrl-V TAB or the equivalent Ctrl-V Ctrl-I; this also works for other special characters.

This works, but is unreliable in scripts and copy-and-paste snippets, so I recommend the ultimate way using bash's escape character variables. They are described (poorly) in the manual; to get the tab you'd do $'\t'. Caution: unfortunately this doesn't work within the double-quoted strings, so if you need tabs and spaces, you have to do things like "a b"$'\t'

schachwizard 06-15-2011 10:37 PM

^ Good advice, przemek. Also, I think this deserves a reward for oldest thread revival in history. :)

przemek.klosowski 06-16-2011 09:36 AM

My pleasure, sir. I revived it because it came up high in Google search so I thought it'd be useful for others.

IanWood 03-30-2012 04:11 AM

It was! Thanks.

David the H. 03-30-2012 06:45 AM

To summarize the solutions given above, and to add one:

Code:

$ echo '[      ]'        # enter a literal tab with <ctrl+v><tab>
[      ]

$ echo -e '[\t]'
[      ]

$ echo $'[\t]'
[      ]

$ printf '[%b]\n' '\t'        # %b interprets backslashes in the same way as -e and $''
[      ]

Note also that these are all true for bash, but may be different for other shells. I believe that echo ksh, for example, automatically interprets backslashes without the use of any options.

Finally, since <tab> is ascii 011 (octal) and x09 (hex), you can also use \011 or \x09 instead of \t. And from bash 4.2+ you can also use the unicode code point \u09.

boyapati_ravi 09-05-2013 07:11 AM

using tab as delimeter for the cut
 
Hi,
use this. it will work

cut -d\t -f1,2


Ensure the input has the tabs not just the spaces.



Hv Gd Time..

Firerat 09-05-2013 07:23 AM

man cut
Code:

      -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter

cut's default delimiter is TAB


and in your code, you were setting as t

not tested.. but I think
Code:

cut -d$'\t'
would do it.. but redundant for the reason above

boyapati_ravi 09-05-2013 07:34 AM

tab as delimeter for the tab
 
Hi,

I tested.Both are working fine.

cut -d\t as well as cut -d$'\t'

As you said it could be redundant. (Any ways it could be useful for other special characters).

thanks,
Hv Gd time..

Firerat 09-05-2013 07:55 AM

Code:

echo -e "t1\tt2\tt3"
echo -e "t1\tt2\tt3" | cut -f2
echo -e "t1\tt2\tt3" | cut -d\t -f2
echo -e "t1\tt2\tt3" | cut -d$'\t' -f2

I have tested and they both do different things
you need to retest

mlg7 10-25-2013 12:26 AM

Code:

$ echo $'a\tb'
a        b


JJJCR 10-25-2013 02:42 AM

another resurrection of old post.

:)


All times are GMT -5. The time now is 07:49 AM.