A Next Generation Blog

Getting BASH on Android

Bash Terminal on Android

Recently we discussed how to get a shell on your Android. That only brings out the Android Shell (ash) into use. But the shell that currently dominates the POSIX world is GNU Bash. Android Shell is powerful with a host of features, yet nothing compared to Bash. In fact it is a subset on Bash, just like the original Bourne shell (sh). Fortunately enough, you can enjoy the same robustness on Bash shell on your Android too. In this post I’ll show you how to get Bash running on your android, and optionally making it the default shell, replacing ash.

But before we start our chore, let us be familiar with our friend Bash. Bash is originally a shell for UNIX and UNIX-Like environments (Macs, Linux etc.) developed by Brian Fox for the GNU Project as a free replacement for the original UNIX Bourne shell (sh) some 23 years ago back in 1989. It is used widely as the shell of choice on Linux and Mac OS X. Also, it has been successfully ported to Windows.

Bash is actually a command processor, typically run in a text window or a terminal emulator that allows the user to type commands perform certain actions. Bash can also read commands from shell scripts and execute them. Like all UNIX shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language are similar to sh and its derivatives, offering a nice interoperability with other popular shells. In addition, it supports history and command auto-complete. These features make it the shell of choice for every command-line junkie.

Since Android is Linux (I can call it Linux!), anything that works on Linux will do on Android. So will Bash. This idea was taken by one developer Mzet at xda-developers.com. He compiled entire the entire Bash shell for android target and statically linked it with its dependencies, making it a single executable.

Now, let’s bring bash to the bot. Be warned that, pushing an executable into system directories needs your device to be rooted. Unfortunately there is no workaround for this. And yes, rooting, jail-breaking and such stuff is dangerous and might brick your device. PROCEED AT YOUR OWN RISK. AT ANY MEANS BLOGZAMANA SHALL NOT BE RESPONSIBLE FOR ANY LOSS OR BRICK OF DEVICE ARISING FROM FOLLOWING THIS TUTORIAL. However, should you fall into any pits, we are there to help you. Just leave a comment, describing your problem.

Procedure

  1. Start by downloading bash from his personal file dump. This is an executable. Since it begins with a text line (ELF, executable marker of linux), browsers may detect it as a text file and try to render it your browser. So save the link target as “bash”. Even some browsers add a “.bin” extension to the file. So, if that is the case, you will either need to rename it or use “bash.bin” in place of simply “bash”.
  2. If you have downloaded into your computer, transfer the file into your device. Note the path of the file you transferred.
  3. Open a command terminal, either adb or an android terminal emulator. Elevate into super user by typing
    su
  4. Mount /system directory in read-write mode. By default /system is mounted in read-only mode. You cannot write anything in this mode.
    mount -o remount,rw /system
  5. Push the file into your /system/bin directory. You can either copy it using a file manager or by using adb (Android Debug Bridge) by elevating adb or a shell:
    cp <path to bash executable> /system/bin
  6. CD to system/bin ditectory
    cd /system/bin
  7. Change the permissions of bash executable to allow execution by any user and modification by super user (root).
    chmod 0755 bash

This is it. You have now successfully installed bash in your android device. Get set and explore bash.

Running bash

To get into bash, type the following in the terminal
bash

After you’re done type
exit
to close bash and
exit
again to close the terminal.

If you wanna make Bash your default shell, replacing your default shell then, you need to do some additional task.

  1. Fire up a terminal and elevate to root.
  2. CD to /system/bin directory.
  3. Create a symlink to bash in the name sh to replace the existing link to ash by typing the following:
    mv sh sh0
    ln -s bash sh

In case you wanna revert, cd to /system/bin and remove the existing symlink
rm sh
mv sh0 sh
and optionally bash.
rm bash

Stay tuned to BlogZamana for some more exciting android tips and tricks.

Credits: mzet at xda-developers.com
10 Comments
  1. Kevin A. says

    Amitosh, sir, I thank you to the highest degree possible for this bit of helpful info! Your site and this article were the first one that I found to explain it just right, real simple and get all the commands and steps involved correct. Of note however, there was a minimal difference on bash in my phone and the destination path where you start it: You have just #Bash however on my phone it was downloaded fine from your link, never renamed it, yet the terminal only accepted this instead —> #bash.bin
    I am running Kitkat Android 4.4.2 and chose ‘Root Browser’ app, to verify the right name in the files properties, so you may look into that small detail. For instance, on my Linux Bash you set it as /bin/bash if the user prefers it. But, other than this, no problems. I am very grateful here because now, I REALLY own my phone!

    Take care. Keep up the awesomeness!

    1. Amitosh says

      The browser has chosen an extension for the binary file it found on the server and renamed it to something with a .bin extension. However, the original file on the server does not have this extension. Thanks for pointing out. I’ll update the article accordingly.

      1. Kevin Alexander says

        Update:

        Actually you are correct I think as the article stands now. I had flashed a new rom today but did not back up any of the previous ROM. So today, trying it just as you have worked. Yes, I also understand what you’re saying about the renaming, and that was the first thing I did in my /sdcard/Download folder was drop the .txt. and that is all.

        Even once in the /system/bin directory right at the end, the bash file had no extra attibutes as it did like I was was saying in the first post.

        Anyway, great work and thanks for sharing – to all that seek answers!

  2. Kevin says

    Hi, I have one question. How do I get common Linux commands for this Android’s shell? For example, trying to change permissions on a folder using: #chmod a+rw -$ /sdcard gives error”
    #Bad mode

    I never see this Bad mode using Bash in Linux. What is this meaning and might you know a way to rephrase the command I gave so it will not give such error?

    1. Amitosh says

      You need to mount sdcard as read-write first
      #mount -o rw,remount /sdcard
      #chmod 777 /sdcard

      Numbers are generally work better.

  3. Kevin says

    Hi, you are so smart about Android. Very impressive. So once bash is installed, I was wondering a couple things: a) how can I get the program bash-completion installed now, or can I? b) So I have downloaded two pretty solid manpage apps, one is for downloading for offline use, the other a standard online type app. So for instance, where would one put the man pages docs in an android file system?
    c) You have discussed here how to make bash the default shell, but that is for the root user, but for the regular user, how can I make bash the default shell there-this is similar to in linux when using a terminal, you can still perform a lot of commands as the regular user, but I would like bash, not ash. Just stick the bash script in the $PATH for the regular user?

    1. Amitosh says

      a) Unfortunately, An android build of bash-completion is not available (as of my knowledge). No one has compiled it yet, in theory should be easy but, you need to deal with cross-compiling,headers, changing platforms. One day I might just compile it, if the code permits.. The code permits! It just a shell script and nothing else. You just need to place them in the proper places.
      Grab the source here: http://bash-completion.alioth.debian.org/
      Run standard config,run,install but by changing the root to some other directory. Now push the changes to the device. You need to make the bash-completion script executable as well.
      Grab the source on a linux system and:
      $./configure –prefix=/somewhere/else/than/usr/local
      $make
      $make install

      After that copy the contents to device.
      Add /etc/bash_completion to bashrc

      b) Android has no equivalent support of man pages as we talk about them in UNIXes. There is no “/etc/man”. The apps you said probably allow you to read linux man pages, either online or by downloading them into some application-specific location somewhere probably in the SD card. That is totally dependent on the app.
      c)Pointing the “/system/bin/sh” symlink to bash does the work. Android doesn’t have separate shells for users.

  4. Kevin Alexander says

    Nice work! Thanks for getting back on that. Will give it a go sometime.

  5. Bangkak says

    After follow your guide my android cannot be rooted, and also terminal and other apps require root access won’t open.

    1. Amitosh says

      What Root Mamager app are you using? And did you make ‘bash’ the default shell by symlinking?
      Some root managers push su to non-standard locations and add that directory to the PATH by setting a ‘ash’ specific configration file. Once you find the location of ‘su’ you can add it to ‘bash_rc’

Leave A Reply

Your email address will not be published.