seanwatson
seanwatson

Reputation: 1073

How to make echo interpret backslash escapes and not print a trailing newline?

I would like to use echo in bash to print out a string of characters followed by only a carriage return. I've looked through the man page and have found that echo -e will make echo interpret backslash escape characters. Using that I can say echo -e 'hello\r' and it will print like this

$>echo -e 'hello\r'
 hello
$>

So it looks like it handled the carriage return properly. I also found echo -n in the man page will stop echo from inserting a newline character and it looks like it works when I do this

$>echo -n 'hello\r'
 hello\r$>

The problem I'm having is in combining both -e and -n. I've tried each of echo -e -n 'hello\r', echo -n -e 'hello\r', echo -en 'hello\r', and echo -ne 'hello\r' and nothing gets printed like so:

$>echo -ne 'hello\r'
$>

Is there something I'm missing here or can the -e and -n options not be used together?

Upvotes: 24

Views: 53788

Answers (5)

Mark Reed
Mark Reed

Reputation: 95252

I think it's working, you're just not seeing it. This is your command:

$> echo -ne 'hello\r' 

Because of the carriage return (\r), that will leave the cursor at the start of the same line on the terminal where it wrote the hello - which means that's where the next thing output to the terminal will be written. So if your actual prompt is longer than the $> you show here, it will overwrite the hello completely.

This sequence will let you see what's actually happening:

echo -ne 'hello\r'; sleep 5; echo 'good-bye'

But for better portability to other shells, I would avoid using options on echo like that. Those are purely bashisms, not supported by the POSIX standard. The printf builtin, however, is specified by POSIX. So if you want to display strings with no newline and parsing of backslash sequences, you can just use it:

printf '%s\r' 'hello'

Upvotes: 23

POSIX 7 says it is not possible:

If the first operand is -n, or if any of the operands contain a backslash character, the results are implementation-defined.

On Ubuntu 12.04 for example:

  • Bash's built-in echo echo -e '\n' interprets the newline, echo '\n' does not.
  • /bin/sh's built-in echo does not have the -e option, and interprets the newline even without it. This can byte when writing Makefiles, which use /bin/sh by default.

POSIX 7 solution: printf as the others mentioned.

Upvotes: 2

David W.
David W.

Reputation: 107040

I'd like you to introduce you to printf.

OP, meet printf. printf. This is the OP...

Whenever you are trying to do anything unusual with output in BASH, you should switch to printf. In fact, I use printf all the time, so my scripts will run under both BASH and Kornshell.

Although BASH and Kornshell are 99% the same, the echo command is different. In Kornshell, it's deprecated, and you're supposed to use the builtin print. However, there's no print in BASH. Using printf solves the problem because it works (sort of) the same in both shells.

Since printf doesn't automatically end each line with a \n, you don't have to worry about how to prevent the \n from being appended. Heck, if you want a \n, you have to put it yourself. In your case:

printf `hello\r`

However, for safety reasons, you should really do this:

printf '%s\r' 'hello'

This is a habit I've gotten into when I'm using printf. Imagine if I want to print out $FOO, and I do this:

printf "$FOO\n"

That will normally work, but what if $FOO has a dash at the beginning, or has a % sign in it. The above won't do what I want to do. However, if I do this:

printf '%s\n' "$FOO"

This will print out $FOO no matter what $FOO has in it.

Upvotes: 7

Keith Thompson
Keith Thompson

Reputation: 263237

There are numerous different implementations of the echo command. There's one built into most shells (with different behavior for each), and the behavior of /bin/echo varies considerably from one system to another.

Rather than echo, use printf. It's built into bash as well as being available as an external command, and its behavior is much more consistent across implementations. (The major variation is that the GNU coreutils printf recognizes --help and --version options.)

Just use:

printf 'hello\r'

Upvotes: 11

Dennis Williamson
Dennis Williamson

Reputation: 360035

Quoting. It's the wave of the future. Always quote.

echo -ne 'hello\r'

Use double quotes if you put a variable inside that you want expanded.

Upvotes: 1

Related Questions