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.
echo -n messageto tell
echonot to append a newline; others use
echo message \cto 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, SteveHowever, 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"
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
$c is set to the empty string.
You can buy the content of this tutorial as a PDF to download to all of your devices!
You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!