I recently installed Ubuntu and when I run sh on the Terminal my arrow keys don't work so I can't go through my call history or edit whatever I'm typing. It just comes out as weird characters like ^[[A.

The login shell is bash and it works fine but as soon as I switch to sh, they don't work.

How can I fix this?

  • 2
    why do you switch to sh? – oenone Sep 30 '11 at 11:00
  • 2
    @oenone Why does tt matter why the OP switches to sh? Surely the reason for why he does that is irrelevant to the fact that it's problematic. – Agi Hammerthief Mar 9 '14 at 11:46

The /bin/sh (dash) shell is a POSIX compliant, reduced functionality shell to be more efficient (smaller) for booting the system. As part of that, history and advanced command-line editing are not included. It is more appropriate for strict POSIX scripting than as an interactive shell. The idea is that the 'login' shell would be bash by default, but the boot system would be using dash. The man page in Ubuntu 11.04 shows that dash has history and command-line editing, but the program does not have these features. Take a look at the size of the base executables.

$ ls -l /bin/*ash /bin/sh
-rwxr-xr-x 1 root root 822420 Mar 31 15:26 /bin/bash
-rwxr-xr-x 1 root root  83848 Nov 15  2010 /bin/dash
lrwxrwxrwx 1 root root      4 May 17 21:15 /bin/rbash -> bash
lrwxrwxrwx 1 root root      4 May 17 21:15 /bin/sh -> dash
$ strings /bin/bash | egrep -ci 'fc|hist'
$ strings /bin/dash | egrep -ci 'fs|hist'

Try to get used to running $SHELL or bash instead of just sh.

| improve this answer | |

In case /bin/sh is Dash, it has to be configured --with-libedit before compilation. Otherwise you can still run set -o vi in the shell but it does not do anything useful.

| improve this answer | |

sh doesn't have any history. At least, mine doesn't:

server$ sh
\h$ history 
sh: history: not found
\h$ fc
sh: fc: not found
\h$ exit
server$ type sh
sh is hashed (/bin/sh)
server$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Jun  1 18:43 /bin/sh -> dash*
| improve this answer | |
  • 1
    Really? According to the IEEE specification it does: pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html – jasonwryan Sep 30 '11 at 6:24
  • @jasonwryan, you'll notice that anything related to history or line-editing mode is marked with a UP in that spec which means it's optional (for POSIX conformance, not Unix conformance). – Stéphane Chazelas Nov 13 '12 at 22:45
  • @StéphaneChazelas - maybe true, but jasonwryan is definitely correct anyway. Here's another link. And besides - are you referring to $FCEDIT or $HISTFILE or even just fc? I thought those at least were mandatory - and the rendering of the $PS1 \!. I could be wrong about the POSIX spec - but they all work in dash and have for years. – mikeserv Jun 10 '14 at 19:29
  • 1
    @mikeserv, not sure what you mean, but to reiterate, all of fc, $FCEDIT, $HISTFILE, $PS1 are optional in POSIX (marked with UP for "User Portability"). The sh of Unix conformant systems will have those as UP is required for Unix conformance (among other things Unix also requires that echo -e outputs -e<LF>). But if a system/shell only claims POSIX conformance, then it doesn't need to implement those. For Debian ash, it may be enabled at compile time with --with-libedit as already mentioned, but Debian at least doesn't. – Stéphane Chazelas Jun 10 '14 at 19:51
  • @StéphaneChazelas - thanks a lot. The dash I use as packaged for Arch is therefore drastically different than Debian's I take it. That's valuable info. Weird that's not in man. – mikeserv Jun 10 '14 at 21: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.