Solved

What does -n do in bash scripting

Posted on 2014-03-11
4
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

735 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