23

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?

αғsнιη
  • 41,407
Joe
  • 231
  • Thank you for the question! I wondered why I didn't have any history, colors etc. in shell sessions on one server, and indeed I had a sh login shell ... Oh my. – Tobias Aug 31 '20 at 13:29

3 Answers3

17

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'
181
$ strings /bin/dash | egrep -ci 'fs|hist'
1

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

Arcege
  • 22,536
10

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.

0

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*
Teddy
  • 1,555
  • 10
  • 13
  • 1
    Really? According to the IEEE specification it does: http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.html – jasonwryan Sep 30 '11 at 06:24
  • 1
    @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
  • 2
    @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
  • 1
    Debian dash 0.5.8-2.1 lists fc in the man page but lacks support for it in practice. I even tried invoking as dash -i and starting with HISTSIZE=10 before trying fc to no avail. There are references to fc in the source code, but when I run e.g. strings /bin/dash (which should include all bulitins … among other things), there is no fc present (nor any matches for "hist"). – Adam Katz Mar 07 '16 at 23:23