UNIX / Linux Shell Scripting Tutorial

Unix / Linux Bourne / Bash shell scripting book Buy this book as a 75-page PDF document.
Read the sample. All the content, available offline, with examples, no adverts, and convenient to print.

UNIX/Linux Shell CheatSheet : free A4 PDF (52Kb) | PNG (90Kb)

Unix / Linux Shell Scripting Tutorial

Echo : -n vs \c

As you may have noticed by now, when you use the echo statement, a newline is added at the end of the command. There is a fix for this ... well, more accurately, there are two fixes for this.

Some Unix systems use echo -n message to tell echo not to append a newline; others use echo message \c to do the same thing:
echo -n "Enter your name: "
read name
echo "Hello, $name"
This will work on some systems, and will look like this:
Enter your name: Steve
Hello, Steve
However, on other systems, you need to write the code like this:
echo "Enter your name: \c"
read name
echo "Hello, $name"
Which will provide the same results for those systems.

Well, that's a pain. Here's a workaround which will work on both:

         if [ "`echo -n`" = "-n" ]; then
	  	n=""
	  	c="\c"
         else
	  	n="-n"
	  	c=""
         fi

  echo $n Enter your name: $c
  read name
  echo "Hello, $name"
If echo -n wasn't interpreted properly, it would just echo out the text -n, in which case, $n is set to the empty string, and $c is set to \c. Otherwise, the opposite is done, so $n is set to -n, and $c is set to the empty string.
Suggested books: Recommended Reading
StumbleUponStumbleUpon | del.icio.usdel.icio.us

Site Links



1. Intro
2. Philosophy
3. A First Script
4. Variables - Part I
5. Wildcards
6. Escape Characters
7. Loops
8. Test
9. Case
10. Variables - Part II
11. Variables - Part III
12. External Programs
13. Functions
14. Hints and Tips
15. Quick Reference
16. Interactive
17. Exercises
18. Forum
19. Recommended Books




My blog has tips about how to use Unix and Linux commands XML



contact