Solved

What does -n do in bash scripting

Posted on 2014-03-11
4
396 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

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

825 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