LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-13-2004, 10:24 PM   #1
Bobb3
LQ Newbie
 
Registered: Oct 2004
Location: montreal
Posts: 1

Rep: Reputation: 0
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-13-2004, 10:47 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
You need to use -e to enable the use of special characters. man echo

For example....
echo -e "test \t\t test"
 
Old 06-15-2011, 01:09 PM   #3
przemek.klosowski
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Rep: Reputation: Disabled
'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'

Last edited by przemek.klosowski; 06-16-2011 at 09:41 AM.
 
4 members found this post helpful.
Old 06-15-2011, 10:37 PM   #4
schachwizard
Member
 
Registered: Sep 2010
Location: Philly
Distribution: OpenSuse
Posts: 67

Rep: Reputation: 3
^ Good advice, przemek. Also, I think this deserves a reward for oldest thread revival in history.
 
Old 06-16-2011, 09:36 AM   #5
przemek.klosowski
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Rep: Reputation: Disabled
My pleasure, sir. I revived it because it came up high in Google search so I thought it'd be useful for others.
 
Old 03-30-2012, 04:11 AM   #6
IanWood
LQ Newbie
 
Registered: Jan 2011
Posts: 5

Rep: Reputation: 0
It was! Thanks.
 
Old 03-30-2012, 06:45 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
1 members found this post helpful.
Old 09-05-2013, 07:11 AM   #8
boyapati_ravi
LQ Newbie
 
Registered: Sep 2013
Posts: 2

Rep: Reputation: Disabled
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..
 
Old 09-05-2013, 07:23 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 09-05-2013, 07:34 AM   #10
boyapati_ravi
LQ Newbie
 
Registered: Sep 2013
Posts: 2

Rep: Reputation: Disabled
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..
 
Old 09-05-2013, 07:55 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 10-25-2013, 12:26 AM   #12
mlg7
LQ Newbie
 
Registered: Oct 2013
Posts: 1

Rep: Reputation: Disabled
Code:
$ echo $'a\tb'
a	b

Last edited by mlg7; 10-25-2013 at 12:28 AM. Reason: tab was not shown in output
 
Old 10-25-2013, 02:42 AM   #13
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Rep: Reputation: 26
another resurrection of old post.


Last edited by JJJCR; 10-25-2013 at 02:42 AM. Reason: edit
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab width in Bash - how do I set it benr77 Linux - General 8 12-04-2012 10:53 AM
FC 4. Modify ALT+TAB behaviour to CTRL+TAB xtracto Linux - Software 1 09-22-2005 02:06 PM
bash "tab tab" - how to turn off? nicflatterie Linux - General 2 04-04-2005 03:09 PM
how to have no interpretation of special characters (ctrl, tab ...) in a script xround Linux - General 1 11-29-2004 07:21 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration