Re: Prompting \n



T o n g wrote:
What I didn't make clear in the OP was that, I was actually looking for a
general purpose command that can show whatever messages thrown to it,
including the common backslash escapes. echo -e does a good job, but

But 'echo -e' isn't portable.

printf will choke on any of its control characters, e.g., %.

In that case use %b and print everything as a string like echo.

$ printf "%b" "Hello world!"

$ printf "%b" "Hello %1 %2 %3"
Hello %1 %2 %3

$ printf "%b" "Hello1 %1\nHello2 %2\nHello3 %3"
Hello1 %1
Hello2 %2
Hello3 %3

Today, I just found that printf cannot be used to show whatever the
command line is.

Yes it can.

Here is an example:

set -- command -opt param params...

echo='echo -e'

$ $echo "\n> $@\n"

> command -opt param params...

That is quite a convoluted example.

So far so good. Now try printf:

$ printf "\n> $@\n"

> command

What's happening? why printf chokes on '-'?

Because $@ is quoted it is being split up into separate arguments. If
you want to use all of the arguments together in a string you need to
use $* not $@. This works as you expect.

$ printf "\n> $*\n"

You can see what "$@" does by using it with a for loop and printing
each argument in turn.

$ for i in "\n> $@\n"; do echo _"$i"_;done
_\n> command_
_-opt_
_param_
_params...\n_

$ for i in "\n> $*\n"; do echo _"$i"_;done
_\n> command -opt param params...\n_

Use "$@" when you want each argument in separate strings. Use "$*"
when you want all of the arguments together in one string.

BTW, the reason that I gave up 'echo -e' was that it started to
mysteriously output that "-e " in front of the messages I wanted to show
in my /bin/sh scripts. I still haven't figure out why yet.

You were probably calling it twice with echo -e -e.

Bob

Attachment: signature.asc
Description: Digital signature



Relevant Pages

  • Re: Persisting env vars in cmd windows
    ... more about batch files as you go on, ... echo The time is %time::=.% ... But the piece de resistance was the "start" command, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... But the piece de resistance was the "start" command, ... - Have all code for the one project in one single batch file. ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... I open several Command windows in succession. ... Drag the name of C000_CreateSymbols.bat into a new command window ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)