0

I used this question to strip the newline from a string, but I've encountered a problem on one host.

For some reason, /bin/echo -n foo prints nothing on that host. I have no idea why that host behaves oddly. /bin/echo foo works fine. It's not the terminal, since echo -n foo > /tmp/bar also shows nothing.

/bin/echo --help says that is should work:

Usage: /bin/echo [OPTION]... [STRING]...
Echo the STRING(s) to standard output.

  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit

% /bin/echo --version
echo (GNU coreutils) 5.97

What can possibly cause this not to work? Could there be some strange buffering in this bash-shell?

Community
  • 1
  • 1
cdunn2001
  • 17,657
  • 8
  • 55
  • 45
  • You could also use `printf`. – devnull Mar 18 '14 at 19:18
  • Yes. And I got it working with `tr -d '\n'`. But I am wondering what's wrong with `echo` on that host. – cdunn2001 Mar 18 '14 at 19:21
  • Who knows? BTW, `echo` is a bash builtin, too. Not sure which one you're using. – devnull Mar 18 '14 at 19:23
  • 1
    Are you sure it prints nothing? Perhaps the output is being overwritten by your shell prompt. Try `/bin/echo -n foo | wc`. (I know you said that `echo -n foo > /tmp/bar` shows nothing; did you check that by running `cat /tmp/bar`?) – Keith Thompson Mar 18 '14 at 19:24
  • @keith-thompson: Yes, I tried `cat`, but I didn't try `wc`. Good idea! The problem is the terminal, not `echo`. Very strange. Might be my `LS_COLORS` or `INPUTRC`. – cdunn2001 Mar 18 '14 at 19:39

1 Answers1

2

Based on information in comments, it appears that /bin/echo -n foo is producing the expected output, but it's being overwritten by your next shell prompt. (Most likely it's your shell, not your terminal settings.)

You can demonstrate this by running

/bin/echo -n foo | wc

which should produce this output:

  0       1       3

You can also try this:

/bin/echo -n foo ; sleep 5

This will delay your next shell prompt for 5 seconds, so you can see the output before it's overwritten.

(What shell are you using? What's the value of $PS1?)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631