122

For example, if I am tail -fing a file or reading user input, <Enter> will produce ^M in the terminal instead of ending the read or adding a newline to stdout. ^J works fine.

I'm sshing into the ubuntu system, if that matters. This happens to me both in zsh and bash. One solution I played with is remapping the ^M to ^J in zsh, but that seems like it wouldn't fix the root problem. Anyone know what might be causing this?

Edit: To answer some questions, I'm sshing into Ubuntu from OSX. I'm using iTerm and zsh. I also forgot to mention that I'm using tmux on the Ubuntu machine.

Edit 2: Missed a question. When I type Ctrl-V Enter I get ^M (both on OSX and Ubuntu).

Edit 3: On OSX and Ubuntu echo $TERM produces screen-256color.

  • What are you Using to ssh to Ubuntu? – Tasos Apr 1 '14 at 2:02
  • Yes --- and from which OS? It seems a TERM problem. – Rmano Apr 1 '14 at 4:41
  • What do you get if you type Ctrl + V followed by Enter? As you are able to execute commands just fine, this is definitely not a problem of the shell. – Adaephon Apr 1 '14 at 7:39
  • 2
    An (involuntary) restart of my OSX box seems to have fixed the issue... I guess it's one of those. Hopefully it's gone for good. – benekastah Apr 1 '14 at 18:41
  • Also for me the only thing that worked was a restart including the clearance of the NVRAM. Press and hold the Command-Option-P-R keys immediately after you hear the startup sound to also reset the NVRAM of OSx – jonbros Jul 7 '15 at 6:56
251

In case anybody else has this problem, it is most likely a problem with the stty terminal line setting rather than a TERM problem. If this happens to you again, try running stty sane and let us know if that fixes it.

| improve this answer | |
  • 37
    This works on OS X as well. – Patrick Berkeley Mar 16 '16 at 19:47
  • 1
    Worked for me (Mint 17.1). – CoderGuy123 Aug 9 '16 at 18:00
  • 1
    Work on Ubuntu Bionic – protoEvangelion May 21 '18 at 2:00
  • 2
    Worked on macOS Catalina ZSH. Question: Why does this only happen sometimes? And does stty sane fix it permanently or will I need to re-run this when/if it happens again? Thanks! – Joshua Pinter Feb 6 at 17:14
  • 2
    @JoshuaPinter: it is a temporary fix since it is caused by programs that do not clean up properly when they exit. – hackerb9 Feb 20 at 4:52
34

Try running stty -a to view your terminal settings. My suspicion is that your icrnl setting is not set and will be shown as -icrnl (the minus sign means that it is turned off) instead of having its usual setting of being on. Here is how my terminal is normally set up when I log in:

$ stty -a
speed 38400 baud; rows 45; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

And I have no problem with line endings: either return (^M) or enter (^J) will end input lines. But if I turn icrnl off then ^M codes suddenly appear each time I am talking to a program and hit enter:

$ stty -icrnl
$ read line
Line of text^M^M^M^M
$ stty -a
speed 38400 baud; rows 45; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

The code icrnl means “turn carriage returns into newlines” and hides from the running program that you might be typing ^M when Unix really wants ^J. Old keyboards used to have a separate Return and Enter key (where Return typically advanced you through a form and Enter submitted it), but today we generally have only one line-ending key and so this terminal setting helps combine the two meanings.

Add the stty icrnl command to your .profile or .bashrc if you find that this is indeed the problem setting.

| improve this answer | |
  • I think I turned off ircnl by accidental key-smashing working in a live terminal session. Just ran $ stty icrnl and it fixed itself. – 0xbe5077ed Dec 7 '19 at 2:02
  • Not sure where to put it, but it did work. Host machine or the one I'm ssh'ed into? Too late now, already fixed it. – Funkodebat May 31 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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