Update mksh to latest version


Question

Android ships with mksh as shell, how can I upgrade to latest version (now R57, mine still on R43)?



Official site https://www.mirbsd.org/mksh.htm offers to compile from source, anyone building ready to run binaries?


Answer

mksh developer here ☺



The problem with distributing binaries is that the binaries need to be built against the exact same bionic libc as your phone provides, which needs both the libc binaries (as shipped on your phone) and the development headers (only present in the AOSP source tree of the person who built your image).



If you wish to use a generic ARM mksh binary instead, you can use one statically compiled against another libc (such as musl (recommended), dietlibc or klibc). However, these will differ in behaviour from the one shipped by Android:




  • Android has a different default PATH, using something like /sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin instead of /usr/bin:/bin

  • Android has different directories for /etc (/system/etc), /tmp (/data/local), and the locations of ~/.mkshrc and default EXECSHELL differ



For these reasons I’d recommend to keep your /system/bin/sh as is and only add a /system/bin/mksh or even /vendor/bin/mksh executable with a newer version if you really need it and use that explicitly in scripts and/or interactively.



If you go to the Debian mksh package page you can download the Debian package for your architecture (arm64 for 64-bit ARM systems, armhf for 32-bit ARMv7 or newer; it’s unlikely you’ll need older ones; 32-bit might work on 64-bit systems but not the other way round), then once downloaded to your local computer, you can extract the package as follows (I’ll use the 32-bit version for this example):



ar x mksh_57-2_armhf.deb
tar xaf data.tar.*
find usr/lib/ -name \*ksh -ls


The last command will list all possible statically-linked-against-not-GNU-libc binaries; for the version 57-2, the output is:



  2633594
 184 -rwxr-xr-x
1 user
user
  185584 Sep 26 00:52 usr/lib/arm-linux-musleabihf/bin/mksh
2633596 124 -rwxr-xr-x 1 user user 126816 Sep 26 00:52 usr/lib/klibc/bin/mksh
2633595 152 -rwxr-xr-x 1 user user 152716 Sep 26 00:52 usr/lib/diet/bin/mksh


So, you can choose any of these three binaries; I really recommend using the musl-compiled one (that is usr/lib/arm-linux-musleabihf/bin/mksh, the path differs for arm64) as it’s the most correct and complete C library.



adb push usr/lib/arm-linux-musleabihf/bin/mksh /sdcard/mksh-new
adb shell
su
mv /sdcard/mksh-new /vendor/bin/mksh
chown 0:0 /vendor/bin/mksh
chmod 555 /vendor/bin/mksh


Now, unless another mksh executable is in the PATH before /vendor/bin/, running mksh script.sh or just mksh (or mksh -l for a login shell) will start the newer binary (but not read /system/etc/mkshrc unless you start it as env ENV=/system/etc/mkshrc mksh), but the system boot process will keep using the version it was designed for and tested with. (Newer mksh versions may contain bugfixes that cause scripts relying on those bugs or older interpretations of the POSIX sh standard to fail.)



As I am also the Debian Developer maintaining the mksh package there, you can consider those packages as official Linux/ARM builds of newer mksh versions (although lacking the Android specifics).



If you really need a binary with the Android specifics, I can access a Debian/ARM porterbox and compile mksh from source, with the different definitions (as listed above), manually and provide it for downloading; contact me (by eMail or IRC) if you need that, as it’s manual effort and costs me extra time.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes