So, I overwrote /bin/bash with a stupid bash script by accident. My previous question can be found below. It was recommended that I create a new one to deal with a problem that arose. To recap, I was able to use the GUI to change my default terminal to dash, where I can run commands. Now, I am attempting to reinstall bash so that I can change my default terminal back. As previously stated, I'm running apt-get --reinstall install bash, which should reinstall it. However, it returns an error:

    reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
Suggested packages:
The following packages will be upgraded:
  bash bash-completion
2 upgraded, 0 newly installed, 0 to remove and 2128 not upgraded.
2 not fully installed or removed.
Need to get 0 B/1,605 kB of archives.
After this operation, 673 kB of additional disk space will be used.
Do you want to continue? [Y/n] (Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 307792 files and directories currently installed.)
Preparing to unpack .../archives/bash_4.4-5_amd64.deb ...
dpkg (subprocess): unable to execute old pre-removal script (/var/lib/dpkg/info/bash.prerm): No such file or directory
dpkg: warning: subprocess old pre-removal script returned error exit status 2
dpkg: trying script from the new package instead ...
dpkg (subprocess): unable to execute new pre-removal script (/var/lib/dpkg/tmp.ci/prerm): No such file or directory
dpkg: error processing archive /var/cache/apt/archives/bash_4.4-5_amd64.deb (--unpack):
 subprocess new pre-removal script returned error exit status 2
dpkg (subprocess): unable to execute installed post-installation script (/var/lib/dpkg/info/bash.postinst): No such file or directory
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 2
Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)

Also, dpkg --configure bash says that it had and error processing the package, and that bash is "in a very bad inconsistent state; you should reinstall it before attempting configuration."

The distribution I an running is Kali 2.2 rolling, and I already know I'm gonna get a lot of hate for this one because if anything, overwriting bash is a mistake.

New Post, as recommeded by @Philippos. Link to old post is here


The problem is that the bash prerm script uses bash so it fails to run causing the whole reinstallation to fail.

There are multiple ways you could get around this:

1) The bash prerm script on my systems (Debian unstable & Debian stable) is incredibly simple and does not need bash to run. So simply edit /var/lib/dpkg/info/bash.prerm and change the first line from #! /bin/bash to #! /bin/sh. Also ensure that /bin/sh is not pointing at bash (ls -l /bin/sh). If it is, you likely have /bin/dash installed, so try editing the prerm file to use that instead.

2) Delete the prerm file and re-install. The prerm file does nothing important in version 4.4-5 on Debian, so you could just get rid of the prerm file and try to reinstall.

3) You can extract /bin/bash from the .deb archive already downloaded. You can see the path in your output: /var/cache/apt/archives/bash_4.4-5_amd64.deb. This is an ar(1) archive containing three files:

  • debian-binary
  • control-tar.gz
  • data.tar.xz

Note that the data.tar file might not be a xz compressed file - there are other formats - gzip, bzip, etc. I'm going to assume it is an xz file, but if not, you can replace that suffix and zcat program appropriately.

You can see the contents of the .deb file by running:

$ ar t /var/cache/apt/archives/bash_4.4-5_amd64.deb

You can see the contents of the data.tar.xz file by running:

$ ar p /var/cache/apt/archives/bash_4.4-5_amd64.deb data.tar.xz | xzcat | tar tvf -

You can extract /bin/bash from that:

$ cd /tmp
$ ar p /var/cache/apt/archives/bash_4.4-5_amd64.deb data.tar.xz | xzcat | tar xvf - ./bin/bash

Since you did that from /tmp. you will now have file /tmp/bin/bash. You can copy that back into /bin and overwrite bash again, but this time with some good content.

There are dpkg tools that you can use to manipulate .deb files, but since that they are simple archives, I find it easier to remember how to use ar(1) and tar(1) as opposed to dpkg-specific options.

  • 2
    Your first method--changing#! /bin/bash to #! /bin/sh in /var/lib/dpkg/info/bash.prerm--is fast, easy, and effective. Kali Linux is a derivative of Debian, where /bin/sh has been a symlink to dash for quite some years now, so no extra steps are necessary. (Anyway, the OP's previous post says they restarted, and they would've gotten way more errors if /bin/sh wasn't working.) I don't have a Kali system but I tested this on Debian 9.2; I would be very surprised if it were not equally effective on Kali. – Eliah Kagan Oct 22 '17 at 5:13
  • One could even temporarily symlink dash to /bin/bash (avoids having to find + modify the install script shebangs). The link will get overwritten by the real bash. (I did it once just to see...) – steeldriver Oct 22 '17 at 11:30
  • Thanks for the long and detailed post! using the 2 last commands (ar p) I get the following errors [tar: this does not look like a tar archive tar: ./bin/bash: Not found in archive tar: exiting with failure status due to previous errors] thanks for all the help! – GarrukApex Oct 22 '17 at 17:38
  • @GarrukApex: Sorry I did not copy the commands from my terminal properly. I left of the "data.tar.xz" argument to the ar command. I have edited my answer. Could you try it again? – camh Oct 22 '17 at 20:11
  • Wow that worked! Thank you so much for your help! – GarrukApex Oct 23 '17 at 1:33

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.