Is there a .bashrc equivalent for android?

Search This thread

cyricc

Member
Aug 1, 2008
35
3
See title. Mostly I just want to have some aliases set every time I open terminal emulator or adb shell. Does anyone know if this is possible?
 

angenoir66

Member
Aug 16, 2007
7
1
Paris area
Hi there,
I'm wondering the same thing.
I've tried to create a bashrc
(/.bashrc ; /system/etc/bash.bashrc ; /root/.bashrc ) But none is working.

I'm about to try a different way,
I've cp /system/bin/sh to /system/bin/sh1
then I'll try to replace /system/bin/sh by a shell script that will launch my aliases then sh1

Something like:
Code:
#!/system/bin/sh1
alias  lsa='ls -Alh '
[...]
/system/bin/sh1

But i'm not pretty sure It could work properly. Firstly because I remember a gain-super-user manipulation that requires to copy and modify rights on sh. (but it could be ok if the script and the copy of sh have the same rights).
Secondly, because I'm not really OK with this; When I'll remove /system/bin/sh in order to replace it, I'll have no shell for a time (if it crashes, I'll not be able to use sh anymore, even with adb).

So if anybody have an other solution it could be damn cool :)
Thank you all

EDIT: I've just tried this. It's ok, I've not lost my shell, but the modifications (one alias, and a path export), are not applied.
 
Last edited:

lbcoder

Senior Member
Jan 21, 2009
2,613
98
I would have thought that it would work with adb, but when I try this:
adb shell /path/to/bash --rcfile /path/to/bashrc
it seems that its a 1-way shell...

well you can always run bash after connecting....
adb shell
/path/to/bash --rcfile /path/to/bashrc

Alternatively, with a little bit of craftiness, you may be able to replace the sh binary with a script that runs bash.
 

shafty023

Senior Member
Jul 24, 2007
1,130
98
Austin,TX
The best way, and what a lot of the ROM devs are doing now is putting a check/call to /system/init.rc and /data/init.rc so we can add our own customizations. This would include boot time chmod/chowns or aliases if need be. Hell we could put a call to /etc/.profile if we wanted and put all our aliases in there. But we can't do any of that till ROM devs put that call in boot.img->boot.ramdisk->init.rc
 

alanthehat

Senior Member
Jan 5, 2013
56
11
HTC Wildfire S
Samsung Galaxy J5
Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
See https://www.mirbsd.org/mksh.htm for the documentation.

Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.
 
Last edited:
  • Like
Reactions: elfaure

elfaure

Senior Member
Yes there is

There is no such file. In fact, the default shell is sh, not bash.

However, there is a solution. Read my post in this thread: http://xdaforums.com/showthread.php?t=518959

Edit /system/etc/mkshrc. You can add your aliases there after the defaults. Type 'alias' from the terminal to see the default aliases that are defined in this file. I believe this file is the master, and when a shell is envoked, the mksh command pipes a hidden copy of this file into the users home directory as .mkshrc for the terminal session.

Also, this is the file to edit to append to your $PATH.
 

Attachments

  • Screenshot_2015-06-14-11-47-23.jpg
    Screenshot_2015-06-14-11-47-23.jpg
    163.7 KB · Views: 1,868
Last edited:

elfaure

Senior Member
Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
See https://www.mirbsd.org/mksh.htm for the documentation.

Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.

/system/etc/mkshrc & ~/.mkshrc

You had the answer right here but you didn't know it
 
  • Like
Reactions: Satsana

rccharles

Member
Sep 30, 2016
6
0
That is interesting. I'll have to try it. I assume it won't work with ADB?

curiously enough my bash aliases work when in the adb shell. I'm running bash on my mac. I guess the alias get expanded by bash before there are sent to adb shell.

Code:
mac $ alias ll
alias ll='\ls -albhFG'
mac $

Code:
mac $ adb shell
shell@htc_a11chl:/ $ ll
drwxr-xr-x root     root              2016-10-03 13:10 acct
lrwxrwxrwx root     root              2016-10-03 13:24 busybox -> /data/data/com.jrummy.app.managerfree/files/busybox
drwxrwx--- system   cache             2016-10-03 14:37 cache
drwxrwx--x system   carrier           2016-09-02 15:24 carrier
dr-x------ root     root              2016-10-03 13:10 config

Please note this does not work:
Code:
mac $ adb shell ll
/system/bin/sh: ll: not found
 

matmutant

Senior Member
Mar 17, 2011
3,395
4,743
~/
andrux-and-me.blogspot.com
Moto G
Moto E
On my side, here is what I did :
Code:
adb root
Code:
adb remount
Code:
adb shell
Code:
vim /etc/mkshrc
I added the following line :
Code:
alias ls='ls --color=auto'

Results attached, now I may play with the PS1 to get colors in the prompt....
 

Attachments

  • mkshrc.png
    mkshrc.png
    81.8 KB · Views: 351

dirtygardner

New member
Sep 5, 2018
1
0
Code:
uname -a: Linux debian 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u5 (2018-09-30) x86_64 GNU/Linux

root@debian:/# adb root		#restart adb with root permissions
root@debian:/# adb remount	#remount partitions on device read-write
remount succeeded
root@debian:/# adb shell	#run remote, interactive shell
device:/ # test0		#execute command test0
/system/bin/sh: test0: not found
127|device:/ # alias test0='date'
device:/ # test0
Wed Oct 10 15:06:16 +00 2018	#SUCCESS


root@debian:/# adb root
adbd is already running as root
root@debian:/# adb remount
remount succeeded
root@debian:/# adb shell
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # echo "alias test1='date'">>/system/etc/mkshrc
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # exit
root@debian:/# adb shell
klteusc:/ # test1
Wed Oct 10 15:10:41 +00 2018


klteusc:/ # exit    
root@debian:/# adb root
adbd is already running as root
root@debian:/# adb remount
remount succeeded
root@debian:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # echo "alias test2='date'">>/system/etc/bash/bashrc
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # exit
root@debian:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
 
Last edited:

pieboots

Member
Aug 1, 2020
27
1
Code:
uname -a: Linux debian 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u5 (2018-09-30) x86_64 GNU/Linux

root@debian:/# adb root		#restart adb with root permissions
root@debian:/# adb remount	#remount partitions on device read-write
remount succeeded
root@debian:/# adb shell	#run remote, interactive shell
device:/ # test0		#execute command test0
/system/bin/sh: test0: not found
127|device:/ # alias test0='date'
device:/ # test0
Wed Oct 10 15:06:16 +00 2018	#SUCCESS


root@debian:/# adb root
adbd is already running as root
root@debian:/# adb remount
remount succeeded
root@debian:/# adb shell
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # echo "alias test1='date'">>/system/etc/mkshrc
klteusc:/ # test1
/system/bin/sh: test1: not found
127|klteusc:/ # exit
root@debian:/# adb shell
klteusc:/ # test1
Wed Oct 10 15:10:41 +00 2018


klteusc:/ # exit    
root@debian:/# adb root
adbd is already running as root
root@debian:/# adb remount
remount succeeded
root@debian:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # echo "alias test2='date'">>/system/etc/bash/bashrc
klteusc:/ # test2
/system/bin/sh: test2: not found
127|klteusc:/ # exit
root@debian:/# adb shell
klteusc:/ # test2
/system/bin/sh: test2: not found

You need to manually edit the mksh file and add your command in for it to persistently work.
Just running the command below will work but will be wiped on reboot.

Code:
alias test1='date'

If you need it to stay persistent over reboots manually edit /system/etc/mkshrc

Code:
## Adding the test0 alias via file editing:
taimen:/ # test0
Wed Aug  5 13:09:46 GMT 2020

## Adding test1 alias via command line:
taimen:/ # alias test1='date'
taimen:/ # test1
Wed Aug  5 13:10:06 GMT 2020

## Reboot Device
taimen:/ # reboot

## Check for persistant changes:
taimen:/ # test0
Wed Aug  5 13:04:24 GMT 2020
taimen:/ # test1
/system/bin/sh: test1: not found

Example file that I used:

Code:
# Copyright (c) 2010, 2012, 2013, 2014
#	Thorsten Glaser <tg@mirbsd.org>
# This file is provided under the same terms as mksh.
#-
# Minimal /system/etc/mkshrc for Android
#
# Support: https://launchpad.net/mksh

: ${HOSTNAME:=$(getprop ro.product.device)}
: ${HOSTNAME:=android}
: ${TMPDIR:=/data/local/tmp}
export HOSTNAME TMPDIR
alias test0="date"
if (( USER_ID )); then PS1='$'; else PS1='#'; fi
PS4='[$EPOCHREALTIME] '; PS1='${|
	local e=$?

	(( e )) && REPLY+="$e|"

	return $e
}$HOSTNAME:${PWD:-?} '"$PS1 "

This will stay persistent on reboot.
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 3
    Yes there is

    There is no such file. In fact, the default shell is sh, not bash.

    However, there is a solution. Read my post in this thread: http://xdaforums.com/showthread.php?t=518959

    Edit /system/etc/mkshrc. You can add your aliases there after the defaults. Type 'alias' from the terminal to see the default aliases that are defined in this file. I believe this file is the master, and when a shell is envoked, the mksh command pipes a hidden copy of this file into the users home directory as .mkshrc for the terminal session.

    Also, this is the file to edit to append to your $PATH.
    1
    Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
    I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
    See https://www.mirbsd.org/mksh.htm for the documentation.

    Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.
    1
    Android's default shell /system/bin/sh is a link to mksh in the same directory. It reads /system/etc/mkshrc & ~/.mkshrc
    I think it also reads ~/.profile & /etc/profile but I'm not sure & I don't know what order.
    See https://www.mirbsd.org/mksh.htm for the documentation.

    Also worth noting is that most, if not all, shells read /etc/profile & ~/.profile so anything in there should be very general.

    /system/etc/mkshrc & ~/.mkshrc

    You had the answer right here but you didn't know it