[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

What does -n do in bash scripting

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
Mark_Co
Asked:
Mark_Co
  • 2
2 Solutions
 
epichero22Commented:
-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
 
Joe JenkinsCommented:
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
 
Joe JenkinsCommented:
Ahh, I was typing my response while epichero was posting their comment.
0
 
Mark_CoAuthor Commented:
Thank you both some much. I have several questions on the subject
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now