How to run shell scripts on Android Devices

12:46 AM , , 0 Comments

Have you ever wanted to be free from performing tedious tasks in Android? Have you ever wanted to write android shell scripts? Well you are not the only one. This article shows you how to write android shell scripts, which helps you in easing your jobs.

Android Shell Script

Uses for Shell Scripts in Android Devices

Writing the android shell scripts have various uses - taking backups of files that you save in android, archiving files stored by another application, etc. As for me, I had to rename the files and folders downloaded with a comic downloader manually and compress each folder individually. So I wrote a simple shell script, that renames the individual folders and creates archives of them.

Prerequisites for running Shell Scripts in Android Devices

If your goal is to write a shell script making use of comprehensive utilities (such as awk, find, etc.), see the Advanced Shell Scripting subsection. Or else, if you just want to write simple shell scripts, see the Simple Shell Scripting subsection.

Advanced Shell Scripting in Android

Since this method is aimed at providing comprehensive abilities for writing shell scripts (utilities such as awk, find, sort, etc.), this requires a rooted android device for this. Rooting an android device is an extensive process, and differs for different devices. How to root your android device is something that you will have to lookup on your own.

BusyBox and Terminal Emulator for Android Shell Script
Busy Box & Terminal Emulator for Android Shell Scripting

In your rooted android device, install an app called BusyBox. It provides most of the *nix utilities such as awk, sed, find, sort, head, etc., which will be useful when you try to write a shell script to run on Android. Then install Terminal Emulator (You can also use other similar tools, but I use Terminal Emulator).


Simple Shell Scripting in Android

This method does not require to root your android device. Just the Terminal Emulator is required to run the shell scripts you created. Refer the link above for getting Terminal Emulator.

Writing Shell Scripts in Android

You can write Shell Scripts in your PC and transfer it to your Android device, or else, write them directly on your Android device. Start writing your android shell script by using the following as the first line (Also known as Shebang)


Running Shell Scripts in Android

Then save your entire script and open Terminal Emulator. Now run the script using sh command.
sh <path to the script>/<script name>

Here is the script that I created:

echo "Voila!! Shell script works on Android"

And here is what I get in the terminal emulator:

Shell Script in Android

Only for rooted devices: If you want the script to be run directly as a command, place the script in /system/bin (or) /system/xbin . Then make the file executable by running the following command:

mount -o rw,remount /system
cp <script_path>/<script_name> /system/bin/<script_name>
chmod 555 /system/bin/<script_name>

For example I have copied the script "" to /system/bin, renamed it to "testmsg" using the following commands:

mount -o rw,remount /system
cp /storage/sdcard0/sh/ /system/bin/testmsg
chmod 555 /system/bin/testmsg (chmod +x did not work, giving me a bad mode error)

Now in the Terminal Emulator, just enter the name of the file and the script will execute.

Shell Script as a command in Android

I hope that wasn't a long winded approach for writing the shell scripts in Android. If you've got other ways for attaining the same result, kindly share them in the comments.

Follow us for more tips and tricks. See the right side pane for our social networking pages.

Shell Script on Android
4 stars - "A very useful article." This article about running shell scripts on android was very useful.

A LINUX enthusiast and tech passionate guy. Love to watch Anime and play cricket. Got extra bit of care in myself, guess that's what people call selfishness :-P Google+