For all android pros out there those who want to make their way through the bot. Don’t you think a terminal would be a good piece to start. Yep, I too agree with you. The flexibility a shell provides is not comparable to any GUI system in the computing world. Using a shell, you can control virtually everything of an operating system which might be possible on the GUI or might be some hidden easter eggs. In fact, a shell is an integral part of every operating system. From the good old days of VT100 terminals, shells have been a standard and still continues to exist on many popular operating systems with sophisticated user interfaces and is used internally for performing all kinds of tasks. And android is no exception. Being a descendant of Linux, Android packs a shell similar to its parent – the UNIX shell. In this post, I’m gonna show you how to get started with your shell.
Getting a shell
There are two ways to get a shell.
-
Using the one that comes with Android SDK.
This shell exists as a part of the Android Debug Bridge (adb) and can be accessed by issuing the command:
adb shell
Along with the shell, the Android Debug Bridge also comes with a host of other utilities and functionalities too. Such a discussion is beyond the scope of this article. As far as my preferences are considered, adb shell is my preferred way of shelling my device. The main advantage is it doesn’t require me to root my device as I hate fiddling with my system. However, the only inconvenience is you need to connect your device to the PC (with USB debugging or remote debugging). Deal with the driver mess (only on Windows; no issues on Linux and Macs, yay!) Setup debugging. Connect adb with an intricate serial number and so on. To get the tools head over to Android SDK Tools from the Android Developers Website . -
Getting a terminal emulator.
The Android platform has also got many terminal emulators that emulate the standard VT100 terminal. Such an emulator is common on *NIX devices. It’s easy to use, portable as well as installs on the device itself. You no longer need a PC for shelling instead, you can even shell on the move. But sometimes some terminal utilities require you to root your device. If you like to get one you can get this very popular Terminal Emulator from the Play Store.
Exploring the shell
Once you have got a shell for yourselves, let us explore it to unlock its potential. Firstly, a word to know about the Android Shell (ash). The Android shell is more or less like the popular GNU bash shell (Bourne–Again Shell) or more appropriately the classic UNIX sh (shell) shell. Yes, for users from a DOS–like background, this is NOTHING LIKE A DOS SHELL. Don’t try to run DOS commands. They would behave strangely or simply won’t work. For example, in UNIX environments the type command lists the full path of an executable; whereas in DOS, it dumps the contents of the file referred. The UNIX equivalent is cat. If you are from a POSIX background, you will be right at home with this shell. Another point to DOS users, all UNIX shell are case–sensitive. Cat is not equivalent to CAT (no command exists!). As a convention, commands are always present in lower case.
Some popular commands are:
- am – Android Activity Manager. It’s used to start and stop Android activities (e.g. applications) from the command line.
- aplay – Command line audio file player. This is the interface of ALSA.
- applypatch – Used to apply patches to android files.
- arec – Command line audio recorder. Part of ALSA.
- bmgr – Backup manager
- cat – Copy the contents of a file to standard output.
- chmod – Change the mode of a file (e.g. whether it can be read or written.) This uses classic UNIX file permissions.
- chown – Change the owner of a file.
- cmp – Compare two files byte–by–byte
- cd – Change the current directory.
- dalvikvm – The dalvik virtual machine. (Used to run Android applications.)
- echo – prints text to stdout.
- export – Makes shell variables available to command–line programs
- date – Prints the current date and time
- dd – Convert and copy a file. By default copies standard in to standard out. This is perhaps the command why I like Linux.
- df – Shows how much space is free on different file systems on your device.
- fsck_msdos – Scan an FAT or FAT32 flie system for errors (like chkdisk on DOS).
- gzip – Compress or decompress a file using GZip (deflate; the one used in ZIP archives) compression algorithm.
- ifconfig – Shows the current configuration of network interfaces (IP, MAC address etc)
- iftop – Shows the current processes using the network interfaces (top, but for networks)
- iptables – Manage the firewall
- kill – Send signals to processes. (SIGKILL, USR1, etc.)
- logcat – Prints the Android runtime log.
- ls – Lists file like the dir command
- make_ext4fs – Format a drive or removable media in ext4 format, the classic Linux filesystem.
- mkdir – Create a directory.
- monkey – A program that sends random events, used to test applications
- mount – Mount a filesystem. Can be used to mount disk images as virtual drives.
- mv – Move a file from one directory to another.
- netstat – Prints network statistics.
- newfs_msdos – Formats a new drive in FAT or FAT32 filesystem.
- ping – Ping a host to determine its reachability or to test network latency;.
- pppd – Starts a PPP link. Used to connect to the internet.
- printenv – Print environment variables.
- ps – List active processes.
- pwd – Print name of the current directory.
- pm – package manager, used to manage installed applications.
- reboot – Reboot or power–off your device.
- rm – Remove (delete) a file.
- rmdir – Delete a directory (only if it is empty).
- top – Shows which processes are currently using the most CPU time.
- umount – Unmount mounted filesystems.
- uptime – Prints how long your device has been running since it was last booted.
- vmstat – Status of Dalvic virtual machine.
- sqlite3 – Administer SQLite databases.
- strace – See what system calls a program makes.
- su – Switch to root user. (only available when your device is rooted)
- set – sets shell variables
This list is not complete. There are more commands available in your device. To know many possible commands you can list the directories in system PATH using the ls command.
Some of the common ones are :
- /sbin
- /bin
- /system/sbin
- /vendor/bin
Note: The exact directories varies from device to device. You can echo the $PATH variable to know what directories are included.
Run :
echo $PATH
to list the directories included in your PATH.
For some more set of *NIX like commands not included in Android, you can get the BusyBox suite for Android. Unfortunately, it requires you to root your device.
Detailed documentation on commands
The Android commands are similar to Linux, UNIX or Mac commands (POSIX). The documentation for any will work for another. The simplest way is to Google for man <command>. And if you have access to a Linux, UNIX or Mac system, fire up a shell and type man <command> to get a detailed documentation about the command too. A thing to note, you will need to use your wisdom, since such documentation is customized to meet the usage of desktops. Here is some documentation about android specific on the Android developers site.
Stay tuned to BlogZamana for more exciting Android tips and tricks. Enjoy Shelling!
Hi,
I was wondering if you know where I should be adding a PATH variable within Android ?
I also read that there is no .bashrc in Android so I was wondering where the PATH variables are located ? Also would you by any chance know where you can make those adjustments within the source code of a ROM (for example CyanogenMod 10) ?
Hi,
As the path is created every time the device is rebooted in the mksh script in /etc/mksh script. You can set the path variable by using the export command (though it might be temporarily) by export PATH $PATH:<your_path_goes_here>
Example:
export PATH $PATH:/sdcard/my_exes
While building a ROM form source code, you can edit your mksh file.
Please see this article too. It covers on getting a bash shell on android