LinuxQuestions.org
Review your favorite Linux distribution.
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 11-24-2009, 07:01 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
how to remove new line in bash using echo


I created a simple bash script. My primary concern is how to remove the new line in the output of my script in /root/test.txt

Synopsis

Quote:
./script.sh foo fee
Content

Quote:
case $1 in
*)
echo -ne "
$1
$2" > /root/test.txt
The output of this is

Quote:

foo
fee
I want to remove the new line above the foo. Is it possible?
 
Old 11-24-2009, 07:40 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use printf instead of echo

Code:
printf "%s\n%s " $1 $2
 
Old 11-24-2009, 07:42 PM   #3
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Remove the new line.
Code:
case $1 in
*)
echo -ne "$1
$2" > /root/test.txt
 
Old 11-24-2009, 07:47 PM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@dxqcanada

Thanks..Forgot that it creates a new line because of '"'
 
Old 11-24-2009, 07:47 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Also note that in your code you explicitly told echo to print out a leading newline:
Code:
echo -ne "\n
$1\n
$2"
the -n option of echo serves to avoid the trailing newline only.

Edit: ooops... too late!
 
Old 11-24-2009, 07:56 PM   #6
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@colucix

yes.. forgot to remove the "-e"

Thanks!
 
Old 11-24-2009, 08:36 PM   #7
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
forgot to tell this.

Can I make it short that instead of writing

Quote:
case $1 in
*)
echo -ne "
$1
$2 $3 $4 $5 $6 $7 $8 $9" > /root/test.txt
Is there a way I can shortened the portion $2 $3 $4 $5 $6 $7 $8 $9 up to variable I want let's say $100.

I though $[2-100] is ok but instead it do an arithmetic which was $98.
 
Old 11-24-2009, 08:48 PM   #8
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
You should read up on using loops ... and shift.

Last edited by dxqcanada; 11-24-2009 at 08:50 PM.
 
Old 11-24-2009, 08:56 PM   #9
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@dxqcanada

Can I use loop inside the echo?
 
Old 11-24-2009, 09:00 PM   #10
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
No.

Kind of like this:



Start the loop as long as there are parameters

echo the parameter ... append to file

shift the parameter to the next

go to beginning of loop
 
  


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
bash script to remove first characters from every line (00) Linux - General 8 08-01-2011 10:28 AM
bash script to remove everything in a line before a space tallmtt Programming 2 12-28-2008 07:40 PM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
Remove file location from bash line okos Linux - Newbie 5 10-30-2007 02:46 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM

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

All times are GMT -5. The time now is 10:23 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