LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH - newbie question regarding line breaks (I think) (https://www.linuxquestions.org/questions/programming-9/bash-newbie-question-regarding-line-breaks-i-think-921242/)

Moymoy 12-30-2011 11:24 AM

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

MTK358 12-30-2011 12:22 PM

So the lines are outputted by your script?

If so, just echo an extra newline after you echo each line.

weibullguy 12-30-2011 12:24 PM

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.

MTK358 12-30-2011 12:26 PM

Quote:

Originally Posted by weibullguy (Post 4561907)
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?

Moymoy 12-30-2011 12:46 PM

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.

weibullguy 12-30-2011 01:02 PM

Quote:

Originally Posted by MTK358 (Post 4561909)
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.

David the H. 12-30-2011 01:21 PM

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. :doh:
http://wiki.bash-hackers.org/commands/builtin/printf

NevemTeve 12-30-2011 02:28 PM

pick one:

Code:

echo "Something\n"
echo -e 'Something\n'


ntubski 12-30-2011 04:26 PM

Quote:

Originally Posted by NevemTeve (Post 4562008)
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'

NevemTeve 12-31-2011 12:41 AM

Yes, you're right, I should have checked before posting, \n doesn't produce newline between "double quotes".

wpeckham 12-31-2011 03:58 PM

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.


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