BASH - newbie question regarding line breaks (I think)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.