15

Very similar to this question.

I'm iterating through a few things with an automated script in BASH. Occasionally the script will come across "-n" and echo will attempt to interpret this.

Attempted this:

 $ POSIXLY_CORRECT=1 /bin/echo -n

and

 $ POSIXLY_CORRECT=1 /bin/echo "-n"

 

But it interpreted the argument each time.

Then this, which works but it's possible to hit escaped characters in the strings, which is why I don't want to apply a null character to all input and use -e.

$ echo -e "\x00-n"
-n

printf is possible, but is to be avoided unless there are no other options (Not all machines have printf as a utility).

$ printf "%s" "-n"
-n

So is there a way to get echo to print "-n"?

1

6 Answers 6

10

printf should be a built-in in all your shells, unless some of your machines have very old shell versions. It's been a built-in in bash for a long time. It's probably more portable than echo -e.

Otherwise, there's really no way to get echo options it cares about.

Edit: from an answer to another similar question; avoid quoting issues with printf by using this handy no-digital-rights-retained wrapper:

ech-o() { printf "%s\n" "$*"; }

(That's ech-o, as in "without options")

6
  • 1
    printf is way more portable than echo, but be sure to pass the string you want to print as an argument, not as a format string (i.e. printf "%s\n" "$stringtoprint" instead of printf "$stringtoprint\n") -- see @djhaskin987's answer. Commented Nov 7, 2012 at 2:01
  • @GordonDavisson: I egotistically added a note from a different answer of mine, which I think is a nice solution.
    – rici
    Commented Nov 7, 2012 at 3:42
  • 3
    Hah! +1 for an actual appropriate use of "$*" Commented Nov 7, 2012 at 8:16
  • 1
    Note that printf is not built-in in pdksh and many of its modern derivatives like mksh. Commented May 2, 2017 at 14:56
  • 1
    Also note that several shells (ksh93, dash, yash at least) won't accept ech-o as a function name. Commented May 2, 2017 at 15:00
4

What about prefixing the string with a character ("x" for instance) that gets removed on-the-fly with a cut:

echo "x-n" | cut -c 2-
4

echo "-n" tells the shell to put '-n' as echo's first argument, character for character. Semantically, echo "-n" is the same as echo -n. Try this instead (it's a POSIX standard and should work on any shell):

printf '%s\n' "-n"
3

If you invoke Bash with the name sh, it will mimic sh, where the -n option was not available to echo. I'm not sure if that fits your needs though.

$ /bin/sh -c 'echo -n'
1
  • That -n is unspecified in the POSIX.2 shell command specification is flatly untrue. Option (non-string) behavior for -n is specified as part of the baseline (non-XSI) standard behavior for echo; it's only shells implementing the optional XSI extensions where it isn't present. See pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html -- now, granted, your /bin/sh may follow XSI, but that's not universally guaranteed. Commented Dec 7, 2014 at 19:49
0

echo "-n" will not print -n because -n is interpreted as an option for echo (see help echo). But you can use:

echo -e "\055n"
1
  • 1
    This is supported with GNU's implementation of echo, but not with any POSIX-compliant version -- an XSI-compliant POSIX implementation of echo would print the literal -e, and a non-XSI one wouldn't interpret the \055. Indeed, if you export POSIXLY_CORRECT=1 on a GNU system, you'll see the former behavior. Commented Dec 7, 2014 at 19:46
-1

Cheating method:

echo -e  "\r-n"
1
  • 1
    ...which works if your output is for human consumption, but not if it's to be read by a program or other non-terminal consumer. Commented Dec 7, 2014 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.