LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2012, 01:37 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
bash, echo redirection not to the last line but first


Hi,

What would be an elegant method to add $var to the beginning of a file?

for instance one does to add a line:
echo $var >> file.txt

echo $var >> file.txt <-- elegant but here at beginning?
so that it reads:
$var


thanks
 
Old 09-03-2012, 01:56 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Using sed:
Code:
sed -i "1i $var" file
Not sure if it would work regardless of the variable's contents. Haven't tested it much yet. 'sed 1s' works as well but it's less easy.
 
Old 09-03-2012, 02:05 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You can't use redirections for this. Not directly, at least.

sed is one way. But ed is another good choice, as it's a proper text editor.

Code:
var='new stuff to insert'

printf '%s\n' '0i' "$var" '.' 'w' | ed -s file
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)

The other option is to do something like this:

Code:
echo "$var" > newfile
cat oldfile >> newfile
mv newfile oldfile
 
  


Reply



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
[SOLVED] multi-line output from a bash script using echo and tr Leo Simon Linux - General 7 08-15-2012 02:05 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
how to remove new line in bash using echo packets Programming 9 11-24-2009 09:00 PM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
TTY echo / redirection / monitor howto rainman4_8 Linux - Newbie 2 06-28-2005 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration