├── .gitattributes ├── screenshots └── prompt.png ├── adb-shell.ps1 ├── adb-shell.sh ├── LICENSE ├── startup.sh └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf -------------------------------------------------------------------------------- /screenshots/prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matan-h/adb-shell/HEAD/screenshots/prompt.png -------------------------------------------------------------------------------- /adb-shell.ps1: -------------------------------------------------------------------------------- 1 | $STARTUP = "/sdcard/.adb/startup.sh" 2 | adb shell "[ -f $STARTUP ] || exit 6" ; 3 | if ($LastExitCode -eq 6){ 4 | Write-Output "file not found, push with adb" 5 | adb push "$PSScriptRoot/startup.sh" $STARTUP 6 | } 7 | if ($LastExitCode -eq 1){ # adb error 8 | exit 1 9 | } 10 | adb shell -t "HOME='/sdcard' ENV='$STARTUP' sh -i -o emacs -o vi-tabcomplete" -------------------------------------------------------------------------------- /adb-shell.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | STARTUP=/sdcard/.adb/startup.sh 3 | adb shell "[ -f $STARTUP ] || exit 6"; 4 | last_exit_status=$? 5 | 6 | if [ $last_exit_status -eq 6 ] ; then 7 | echo "file not found, push with adb" 8 | adb push "$(dirname "$0")/startup.sh" $STARTUP 9 | 10 | elif [ $last_exit_status -eq 1 ]; then # adb error 11 | exit 1 12 | fi 13 | # for most shells, '-o emacs -o vi-tabcomplete' is the default, but for some it isn't. 14 | adb shell -t "HOME='/sdcard' ENV='$STARTUP' sh -i -o emacs -o vi-tabcomplete" 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 matan h 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /startup.sh: -------------------------------------------------------------------------------- 1 | # this file should be in "/sdcard/.adb/startup.sh" 2 | # shellcheck shell=ksh 3 | ENABLE_EXPERIMENTAL_HISTORY=false # Set to true to enable the experimental history workaround 4 | 5 | # basic: 6 | # shellcheck disable=SC2164 7 | cd "${OLDPWD:-$HOME}" # always start from $OLDPWD or $HOME 8 | 9 | ## keys 10 | bind '^L=clear-screen' # ctrl+l => clear screen 11 | bind '^XA=search-history-up' # up-arrow => zsh-like search 12 | # color config 13 | alias ls='ls --color=auto' 14 | 15 | ## prompt 16 | RED=$'\E[1;31m' 17 | GREEN=$'\E[0;32m' 18 | BLUE=$'\E[1;32m' 19 | DARKBLUE=$'\E[1;36m' 20 | ESC=$'\E[0m' 21 | if [ ! "$USER" ]; then 22 | USER="$(logname)" 23 | fi 24 | 25 | if [ ! "$HOSTNAME" ]; then 26 | HOSTNAME="$(hostname -s)" 27 | fi 28 | # shellcheck disable=SC2025 29 | # make the prompt 'user@hostname:$PWD' when user is green by default and red on errors. 30 | 31 | PS1="\$(if [ \$? -ne 0 ]; then print '$RED';else print '$GREEN';fi)$USER$BLUE@$HOSTNAME$ESC:$DARKBLUE\$PWD$ESC\$ " # the start is red if the last exit code is not 0. 32 | 33 | ## fix common mistakes and typos 34 | alias sl="ls" 35 | alias "cd.."="cd .." 36 | alias ".."="cd .." 37 | 38 | # shortcuts 39 | alias l="ls" 40 | alias ll="ls -lh" 41 | alias la="ls -lAh" 42 | 43 | alias rd="rmdir" 44 | alias md="mkdir" 45 | 46 | alias cls="clear" # from windows 47 | alias mkdirs="mkdir -pv" # Create directories recursively with verbose 48 | alias rmtree="rm -r" # remove folder recursively 49 | alias untar="tar -xvf" # see xkcd:1168:tar 50 | 51 | # mksh fixing: readline options can make multi-line editing glitches 52 | _vi(){ 53 | set +o emacs +o vi-tabcomplete 54 | vi "$@" 55 | set -o emacs -o vi-tabcomplete 56 | } 57 | 58 | alias vi=_vi 59 | 60 | # CD ENV : search in the popular folders before error not found 61 | CDPATH=":/sdcard:/sdcard/Android/data:/" 62 | 63 | 64 | 65 | # a function to help users find the local ip (from gh:omz/sysadmin plugin) 66 | myip(){ 67 | if [ -x "$(command -v 'ip')" ]; then 68 | ip addr | awk '/inet /{print $2}' | grep -v 127.0.0.1 69 | else 70 | ifconfig | awk '/inet /{print $2}' | grep -v 127.0.0.1 71 | fi 72 | } 73 | if [ "$ENABLE_EXPERIMENTAL_HISTORY" = true ]; then 74 | # experimental history workaround (its disabled by default in mksh for android. see android.stackexchange:152061 mirabilos answer) 75 | HISTFILE="$HOME/.adb/.history" 76 | # "print -s": print to the history file, which doesn't exist. This allows the shell history features. 77 | if [ -f "$HISTFILE" ] ; then 78 | while read -r p; do print -s "$p"; done < "$HISTFILE" 79 | fi 80 | 81 | add_to_history(){ 82 | # fc -ln -1: get the last command from history without numbers. 83 | last_command=$(fc -ln -1 2> /dev/null)||return 84 | 85 | last_line=$(tail -n 1 "$HISTFILE" 2>/dev/null || true) 86 | if [ "$last_command" != "$last_line" ]; then 87 | # Append the last command to the file 88 | echo "$last_command" >> "$HISTFILE" 89 | else 90 | return 91 | fi 92 | } 93 | PS1="$PS1\$(add_to_history)"; 94 | fi -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Better `adb shell`. 2 | This repo uses the options of [mksh](https://manpages.ubuntu.com/manpages/xenial/man1/mksh.1.html) (the android default shell) to get a better shell. 3 | 4 | ## Features 5 | 6 | ### General 7 | * Colored prompt with path and color difference between successes and fail statuses: 8 | 9 | ![screenshot-of-prompt](screenshots/prompt.png) 10 | 11 | * Keyboard shortcuts, such as `ctrl+l` to clear the screen (in normal `adb shell` it's a shortcut for 'enter' for some reason). 12 | * `CDPATH`: you can `cd` to any directory in `/sdcard`, `Android/data` or `/` without typing the full path 13 | (for example `cd com.android.chrome` even when you are in `/` directory) 14 | 15 | ### ls aliases 16 | * `ls` with color by default. 17 | * shortcuts like `ll` and `la`. 18 | * one alias to fix the typo `sl` => `ls`. 19 | 20 | ### More aliases 21 | * basic commands: `rd` (`rmdir`) and `md` (`mkdir`). 22 | * `cls`(clear the screen) 23 | * `rmtree` (remove folder) 24 | * `..` and `cd..` => `cd ..` 25 | * `untar` (=> `tar xvf`) 26 | 27 | ## Installation 28 | clone this repo: 29 | ```bash 30 | git clone https://github.com/matan-h/adb-shell 31 | ``` 32 | ### Linux/macOS 33 | `chmod +x` and run the `adb-shell.sh` file 34 | (it's a short file, highly recommend you go and read [it](https://github.com/matan-h/adb-shell/blob/main/adb-shell.sh) [and possibly also the `startup.sh` file] before executing) 35 | ```bash 36 | chmod +x adb-shell.sh 37 | ./adb-shell.sh 38 | ``` 39 | This will create the startup file for the `mksh` shell if it doesn't exist, and start the better `adb shell`. 40 | 41 | To make this `adb-shell` globally accessible ether add this to your `$PATH` or add an `alias` to this file: 42 | ```bash 43 | # add this to your .rc file 44 | alias adb-shell="/adb-shell.sh" 45 | ``` 46 | ### Windows 47 | Run the `adb-shell.ps1` script (it's a short file, highly recommend you go and read [it](https://github.com/matan-h/adb-shell/blob/main/adb-shell.ps1) [and possibly also the `startup.sh` file] before executing) 48 | ```powershell 49 | powershell -executionpolicy bypass -File .\adb-shell.ps1 50 | ``` 51 | To make this `adb-shell` globally accessible ether add this to your `$PATH` or add an `alias` to this file: 52 | 53 | ```powershell 54 | # add this to your powershell $PROFILE file (most likely $HOME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1) 55 | set-alias "adb-shell" "\adb-shell.ps1" 56 | ``` 57 | 58 | 59 | ## History experimental feature 60 | [history is disabled by default in the android shell build options](https://android.stackexchange.com/a/152093). 61 | 62 | However, It's possible to emulate that using the shell functions (e.g. write the last command to a history file after every command.) 63 | The history is currently in testing state. If you want to enable it, go to `startup.sh` and change `ENABLE_EXPERIMENTAL_HISTORY` to `true`. (then `adb push startup.sh /sdcard/.adb/startup.sh`) Please open a GitHub issue for any bug/problem/suggestion. 64 | 65 | ## Advanced Installation 66 | 67 | The `adb-shell.sh/.ps1` just pushes `startup.sh` and starts `adb shell` with the config file. 68 | That means, you can skip this automated script, and configure it manually, using: 69 | ```shell 70 | STARTUP=/sdcard/.adb/startup.sh 71 | alias adb-shell="adb shell -t \"HOME='/sdcard' ENV='$STARTUP' sh -i\"" 72 | ``` 73 | The `startup.sh` is actually `.mksh` file for android, so if you want to [config](http://www.mirbsd.org/mksh-faq.htm) [it](https://github.com/MirBSD/mksh/blob/master/dot.mkshrc) [manually](http://www.mirbsd.org/htman/i386/man1/mksh.htm), you can. 74 | 75 | When you want to apply changes, run the following command: 76 | `adb push startup.sh $STARTUP` 77 | 78 | # Contribute 79 | On all errors, problems or suggestions please open a [GitHub issue](https://github.com/matan-h/adb-shell/issues) 80 | 81 | If you found this script useful, it would be great if you could buy me a coffee: 82 | 83 | Buy Me A Coffee --------------------------------------------------------------------------------