RSS Feed Share on Twitter

All Shell Scripting Tips

22 Apr 2018

Newline in Echo: When to use -n, when to use \c

echo: -n or \c

Fortunately, this is becoming less of a problem, but it is worth being aware of, particularly if you are writing scripts for older Unix systems.

When you use the echo statement, a newline is added at the end of the command. That is to say, if your script looks like this:

#!/bin/sh
echo Hello
echo World

Then the output will be:

Hello
World

and not:

HelloWorld

Often, that is exactly what you want, but sometimes you want the echo to not insert that newline automatically.

There is a fix for this ... well, more accurately, there are two fixes for this.

First, you need to be aware that echo is implemented by most shells (as a shell builtin command) and also by the operating system (typically as /bin/echo). So the exact behaviour depends upon what shell you are using, whether it's set to use the builtin echo or the native /bin/echo, and in that situation, you need to know what operating system your script is running on, and how it implements this feature.

Some implementations use echo -n message to tell echo not to append a newline; others don't have -n, so you have to use echo message \c to do the same thing. Most of the time, you will want echo -n, but if you need your script to be fully portable, you need to allow for both:

#!/bin/sh
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, it will look like this, which is not ideal:

-n Enter your name: Steve
Hello, Steve

On other systems, you need to write the code like this:

echo "Enter your name: \c"
read name
echo "Hello, $name"

This will provide the right results on those systems, but may give the wrong output on other systems (where \c does not have any special significance, it will be interpreted as a literal c character).

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 by echo, it would just display the literal text -n. If so, $n is set to the empty string, and $c is set to \c.

In this instance, the echo $n "Enter your name: $c" command will be parsed as:

echo -n "Enter your name: "

Otherwise, echo did interpret -n as an argument telling it to change its behaviour, so $n is set to -n, and $c is set to the empty string. The echo $n "Enter your name: $c" command will be parsed as:

echo "Enter your name: \c"

Either way, the desired result has been achieved.

 

 


You can buy the content of this tutorial as a PDF to download to all of your devices!

Contact

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!