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"?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Cellivar
  • 568
  • 10
  • 27

6 Answers6

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")

Community
  • 1
  • 1
rici
  • 234,347
  • 28
  • 237
  • 341
  • 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. – Gordon Davisson Nov 07 '12 at 02:01
  • @GordonDavisson: I egotistically added a note from a different answer of mine, which I think is a nice solution. – rici Nov 07 '12 at 03:42
  • 3
    Hah! +1 for an actual appropriate use of `"$*"` – Gordon Davisson Nov 07 '12 at 08:16
  • 1
    Note that `printf` is not built-in in pdksh and many of its modern derivatives like `mksh`. – Stephane Chazelas May 02 '17 at 14:56
  • Note that in POSIX shells, the expansion of `$*` is dependant on `$IFS`. Use `echo() (IFS=' '; printf '%s\n' "$*")` to make sure the arguments are joined with spaces even in contexts where `$IFS` may have been modified. – Stephane Chazelas May 02 '17 at 14:58
  • 1
    Also note that several shells (`ksh93`, `dash`, `yash` at least) won't accept `ech-o` as a function name. – Stephane Chazelas May 02 '17 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-
vladz
  • 327
  • 1
  • 4
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"
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
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'
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • 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 http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html -- now, granted, _your_ `/bin/sh` may follow XSI, but that's not universally guaranteed. – Charles Duffy Dec 07 '14 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"
Radu Rădeanu
  • 2,642
  • 2
  • 26
  • 43
  • 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. – Charles Duffy Dec 07 '14 at 19:46
-1

Cheating method:

echo -e  "\r-n"
user3183018
  • 354
  • 1
  • 9
  • 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. – Charles Duffy Dec 07 '14 at 19:44