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 12-30-2011, 11:24 AM   #1
Moymoy
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
BASH - newbie question regarding line breaks (I think)


Hi folks.

I'm pretty new to the whole BASH programming and i've come unstuck on something that seems pretty simple.

Basicly, im trying to insert a break between lines, as when I run my script, I basicly get a wall of text, and would ideally like to space it out a little more to allow for easier reading.

In a nut shell, my current returns are as follows:

This is the question I am asking
this is echoed response AAAA
this is echoed response BBBB
this is echoed response CCCC
this is echoed response DDDD

As you can see, it's a little cluttered, this in an entire script is even worse, so ideally i'd like to have the script produce the results as such:

This is the question I am asking

this is echoed response AAAA

this is echoed response BBBB

this is echoed response CCCC

this is echoed response DDDD

So on an so forth. I've looked at this Sed command, but that seems to run on its own seperate script and im not sure on how to incorperate it as each line break is in a different location.

Is there a symbol or something that can be used to physically insert a break?

Many thanks.

-M
 
Old 12-30-2011, 12:22 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So the lines are outputted by your script?

If so, just echo an extra newline after you echo each line.
 
Old 12-30-2011, 12:24 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Since you haven't provided a snippet of your script showing how you output the text, I will presume you are using the echo command. There are two ways you could double space your output. First, echo a blank line.
Code:
echo "this is echoed response AAAA"
echo ""
echo "this is echoed response BBBB"
echo ""
echo "this is echoed response CCCC"
echo ""
echo "this is echoed response DDDDD"
The other way is to use the -e option so echo interprets backslash escapes and then use the new line escape.
Code:
echo -e "this is echoed response AAAA \n"
echo -e "this is echoed response BBBB \n"
echo -e "this is echoed response CCCC \n"
echo -e "this is echoed response DDDD \n"
Try looking at the man page for the commands you are using....they are a wealth of information.
 
1 members found this post helpful.
Old 12-30-2011, 12:26 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by weibullguy View Post
The other way is to use the -e option so echo interprets backslash escapes and then use the new line escape.
Code:
echo -e "this is echoed response AAAA \n"
echo -e "this is echoed response BBBB \n"
echo -e "this is echoed response CCCC \n"
echo -e "this is echoed response DDDD \n"
Why the spaces before the "\n"s?
 
1 members found this post helpful.
Old 12-30-2011, 12:46 PM   #5
Moymoy
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
My many many thanks, I was looking all through the man pages for something which may be related to 'new line' and completely overlooked echo, as you rightly suggested, turns out that the echo man page described everything you did.
 
Old 12-30-2011, 01:02 PM   #6
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Quote:
Originally Posted by MTK358 View Post
Why the spaces before the "\n"s?
Because my fingers always put a space between "words" when I'm typing before my brain catches up. The space isn't needed.
 
Old 12-30-2011, 01:21 PM   #7
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
For more powerful printing control, consider using the printf command instead.

Code:
printf "this is echoed response %s\n\n" AAAA BBBB CCCC DDDD
The first argument to printf is the format the output should be in, with each argument you want to print being accounted for with a "%" parameter. In bash you'll probably use "%s" (strings) or "%d" (digits) most often. The remaining arguments are the inputs that will be output in the given format. It will print as many times as necessary to consume all the inputs, so you'll get 4 lines here.

I've never come across any really good tutorials on printf in bash, but the documentation for gawk's printf function is very good. Just remember that not everything it says is applicable to bash (bash can't handle floating point numbers, for example).

http://www.gnu.org/software/gawk/man...de/Printf.html


Edit: I knew I should've checked the bash-hacker's wiki first.
http://wiki.bash-hackers.org/commands/builtin/printf

Last edited by David the H.; 12-30-2011 at 01:38 PM. Reason: fixed url + added
 
Old 12-30-2011, 02:28 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
pick one:

Code:
echo "Something\n"
echo -e 'Something\n'
 
Old 12-30-2011, 04:26 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by NevemTeve View Post
pick one:

Code:
echo "Something\n"
echo -e 'Something\n'
Code:
$ echo "Something\n"
Something\n
$ echo -e 'Something\n'
Something

$
Always pick #2 then? Maybe you were thinking
Code:
echo $'Something\n'
 
Old 12-31-2011, 12:41 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, you're right, I should have checked before posting, \n doesn't produce newline between "double quotes".
 
Old 12-31-2011, 03:58 PM   #11
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
when you echo

Code:
echo "something good
"
results in the "something good" on one line, followed by a blank line.

Code:
 echo "something good

"
provides for double blank lines or deals with cases where the next output stacks onto the same line.
 
  


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 shell question, variable sustitution $1... command line args mnemry Linux - Newbie 8 04-19-2011 04:40 PM
[SOLVED] vim remove line breaks ColInvictus Linux - Newbie 3 09-27-2009 08:10 PM

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

All times are GMT -5. The time now is 02:42 AM.

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