Where should I install Toybox? I own a rooted Samsung Galaxy S Relay 4G running stock Android 4.1.2.

If you have Android 6.0 "Marshmallow" or better, then you have Toybox.  This is a package of command-line tools, handy for advanced users who sometimes use a terminal emulator.  Unfortunately, Android 4.1.2 does not include Toybox. Instead, it includes some other tools which aren't as nice to use.

Sometimes I use the terminal emulator written by Jack Palevich; other times I use adb shell from a laptop.


Would it make more sense to install Toybox to my system partition, or to my data partition?  Both partitions have far more than enough free space.
When a command is provided both by Android and by Toybox, I want the Toybox version to win.  On Linux, I might install Toybox to /home/unforgettableid/bin or /usr/local or /opt.  Could you please suggest a sensible place for me to install Toybox (and its large collection of symlinks) on my phone?


[Edit:  I wonder what the various BusyBox installers do.  Also, I wonder how Android sets the PATH environment variable.  As well, I also wonder whether or not there's a way for me to change that variable and to make my change persist across reboots.  Finally, I wonder whether or not putting the Toybox tools first in my PATH would be likely to break things in non-obvious ways.]



Possibly related:  "Android Folder Hierarchy".
Where should I install Toybox?


  Would it make more sense to install Toybox to my system partition, or to my data partition?


Depends. If you don't plan to factory reset the phone ever again, you can choose data partition, else, choose system partition.


  When a command is provided both by Android and by Toybox, I want the Toybox version to win.


The PATH variable contains the location of the directories containing executable binaries. Should you decide to keep a separate directory for your symlinks, configure $PATH to search for a command into your directory first. If it finds one, it will execute it.


  On Linux, I might install Toybox to /home/unforgettableid/bin or /usr/local or /opt. Could you please suggest a sensible place for me to install Toybox (and its large collection of symlinks) on my phone?


All those locations don't exist for Android. Since you're using Android 4.1.2, I suppose you would be having your external SD card formatted with a  FAT filesystem. Symlinks don't work on FAT filesystems (not on FAT32 or vfat at least) so your best bet is to install under /data or /system.

Alternatively, you can make an image under your external SD card, format that image with a filesystem supported by your Android's kernel (often EXT2 | EXT3 | EXT4), then mount it in the device anywhere in loopback mode. I've not tried this method though.


  I wonder what the various BusyBox installers do


Busybox (by Stericson) places the busybox binary into /system/xbin and creates  symlink for its applets to its binary under the same directory. 


  Also, I wonder how Android sets the PATH environment variable. As well, I also wonder whether or not there's a way for me to change that variable and to make my change persist across reboots


I do not know when Android sets up $PATH. However, the shell you're using  would most likely be having a configuration file. 


mksh (MirBSD Korn Shell; the default shell on Android) has a configuration file mkshrc in /system/etc. You can set $PATH there and the change would be persistent across reboots. 
For bash shell, the configuration file is bashrc located under /system/etc/bash.


It appears that both files are meant for interactive sessions only, so if you attempt to use adb for accessing a Toybox or its symlink which is not placed under /sbin, /vendor/bin, /system/sbin, /system/bin or /system/xbin (the default locations in $PATH), you would receive a "COMMAND: not found" error. This is because the default $PATH would be used instead.

I've not found a solution for this problem yet, but see the comments below this answer.

Anyhow, let's install Toybox for now.



How do I install Toybox on an Android device?

Note: the solution assumes that your Android is rooted, that adb is setup in PC, and that your device is connected to a PC with USB debugging enabled.

Installation into system partition

No change in $PATH is required if you choose a standard location. Busybox prefers /system/xbin.


Remount the system partition in rw mode. If required, see Can&#39;t remount /system in rw
Copy the toybox binary to /system/xbin/ and give executable permission to it. (The default permissions set for other binaries is rwxr-xr-x.)

You can do so using chmod

adb shell su -c "chmod 755  /system/xbin/toybox"

Create symlinks for various tools accessible by toybox binary. It is up to you how you create them. 

I used a very simple method, but it requires a *nix system

adb shell toybox | sed 's/\ /\n/g' | head -n -1 | tr -d '\r' | while read line; do adb shell su -c 'ln -sf /system/xbin/toybox "/system/xbin/$line"'; done


All I'm doing here is executing the toybox binary, replacing the white-spaces with a new line so that each tool shows up in a new line. Later on I'm removing the last line (it was blank in output), removing carriage return conveniently put by adb, and then for each line of output I'm creating a symlink to toybox binary with the name of the tool, in the same directory.

You can use this method if you don't have a *nix OS in PC:

adb shell
su
toybox | toybox sed 's/\ /\n/g'| toybox sed '$d'| while read line; do  toybox ln -sf /system/xbin/toybox /system/xbin/$line; done



Done! Your Toybox and its applets are ready to be executed.

Installation into data partition


Create a directory under /data (/data/my_bin henceforth)
Copy the toybox binary to /data/my_bin and give executable permission to it. (The default permissions set for other binaries is rwxr-xr-x.)

adb shell su -c "chmod 755  /system/xbin/toybox"

Create symlinks for various tools accessible by toybox binary. It is up to you how you create them. 

I used a very simple method, but it requires a *nix system

adb shell su -c "/data/my_bin/toybox" | sed 's/\ /\n/g' | head -n -1| tr -d '\r'| while read line; do adb shell su -c "ln -sf /data/my_bin/toybox '/data/my_bin/$line'"; done


Only the location has changed, the rest of the procedure is alike to one followed for installation in system partition
Edit your default shell's configuration file and add

PATH=/data/my_bin:$PATH 


This would cause the shell to look into /data/my_bin first and execute the binary, if available.


Done! Enjoy your Toybox and its applets.