Fedora Linux Support Community & Resources Center
Old 8th August 2007, 05:12 PM
tonytiger Offline
Registered User
Join Date: Jun 2004
Posts: 168
How to write to file from bash

I need to append text to the end of file to the same line from shell.
This works almost:
[Tony@localhost ~]$ echo foo1 >> foo.txt
[Tony@localhost ~]$ echo foo2 >> foo.txt
[Tony@localhost ~]$ cat foo.txt
[Tony@localhost ~]$
However, I would like the result to be
[Tony@localhost ~]$ cat foo.txt
foo1foo2[Tony@localhost ~]$
I believe the difference is because it seems that ">>" makes a new line after writing the text to file.

How to do this? I'm sure it's very simple.


Reply With Quote
Old 8th August 2007, 06:51 PM
erikd Offline
Registered User
Join Date: Mar 2005
Posts: 60
Use 'echo -n'.
Reply With Quote
Old 8th August 2007, 08:25 PM
Hlingler Offline
Join Date: Sep 2006
Location: Connellsville, PA, USA
Posts: 11,307

I'm curious about this, too. If I use the "echo -n" command, will any commands that follow be executed? For example, I have added the following to /etc/rc.local:
{ date; hdparm -S60 /dev/sda; hdparm -S60 /dev/sdb; hdparm -d 1 /dev/hda; } >> /var/log/hdparm.log
but each of the commands is logged on a separate line, with an extra line between. I would like to have them all logged on the same line. Would
echo -n `date; hdparm -S60 /dev/sda; hdparm -S60 /dev/sdb; hdparm -d 1 /dev/hda;` >> /var/log/hdparm.log
accomplish this? I.E., will these commands actually be executed, or just the output echoed to the log file?

Reply With Quote
Old 9th August 2007, 02:51 AM
RupertPupkin Offline
Registered User
Join Date: Nov 2006
Location: Detroit
Posts: 6,607
You could use echo -n, as erikd suggested, though you will need to surround the text with quotes with a space at the end, otherwise you get "foo1foo2" instead of "foo1 foo2"
echo -n "foo1 " >> foo.txt
echo -n "foo2 " >> foo.txt
or you could pipe the text to tr to replace the newlines with spaces:
echo foo1 | tr '\n' ' ' >> foo.txt
echo foo2 | tr '\n' ' ' >> foo.txt
Reply With Quote
Old 9th August 2007, 04:09 PM
tonytiger Offline
Registered User
Join Date: Jun 2004
Posts: 168
Thank you all. It worked.

Reply With Quote

bash, file, write

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot write a file northwestdev Using Fedora 1 24th July 2009 09:03 PM
how i can write file on Usbflash fat file system? sharin00 Hardware & Laptops 6 21st February 2009 10:17 PM
newbie: different ways to write bash variable gold Using Fedora 8 16th December 2007 03:39 PM
bash file? liba Using Fedora 2 10th December 2006 02:45 PM
Bash script - File search output to new file ampapa Using Fedora 12 19th October 2006 01:49 AM

Current GMT-time: 13:10 (Saturday, 19-08-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat