Solved

What does -n do in bash scripting

Posted on 2014-03-11
4
393 Views
Last Modified: 2014-03-11
What does the -n after the echo mean and do and what happens if i exclude it?

echo -n "Enter no 1 : "
read no_1
echo -n "Enter no 2 : "
read no_2
echo -n "Enter no 3 : "
read no_3
echo $no_1 > no_file
echo $no_2 >> no_file
echo $no_3 >> no_file
sort -n -r no_file |sed -n '1p’

Open in new window

0
Comment
Question by:Mark_Co
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:epichero22
epichero22 earned 250 total points
ID: 39921326
-n prevents the script from outputing the trailing newline, and I believe it will allow the user to input data on the same line rather than on a new line, if that's automatically generated.  Example:

Enter no 1 : ___

vs

Enter no 1 :
___
0
 
LVL 5

Accepted Solution

by:
Joe Jenkins earned 250 total points
ID: 39921353
It literally means, "Do not output the following trailing new line"

Bash man echo

If you leave it off it should go to the next line after the colon.  Leaving the -n in would allow the output to stay on the same page.

The output from your script would look like this with it:
Enter no 1 : <input>
Echo no 2 : <input>
Echo no 3 : <input>

Without it:
Enter no 1 :
<input>
Echo no 2 :
<input>
Echo no 3 :
<input>

I don't have access to a linux box at the moment but I'm fairly certain that is how it will output.
0
 
LVL 5

Expert Comment

by:Joe Jenkins
ID: 39921358
Ahh, I was typing my response while epichero was posting their comment.
0
 

Author Closing Comment

by:Mark_Co
ID: 39921424
Thank you both some much. I have several questions on the subject
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now