Bash shell settings in Android 4.0.* (~Ice Cream Sandwich)

I don't know about earlier releases, but ICS has a bash shell in /system/bin. If you start a shell through ADB (eg. ./adb shell), it'll start /system/bin/sh as a shell which is a symlink to bash. Unfortunately ADB does not add the -l option to the shell's commandline, thus the shell will not be a login shell and it'll not look for _any_ configuration files. Sad

The good news is that you just have to execute the shell again with the -l switch (either sh -l or bash -l) and it'll read/execute a few config files. Smile I've compiled strace to be able to get the list of actual pathes that bash looks in for config files.
These locations are:
/.bash_login
/.bash_logout
/.bash_profile
/.profile
/etc/profile
/system/etc/bash/bash_logout
$HOME/.bash_login
$HOME/.bash_logout
$HOME/.bash_profile
$HOME/.profile

Of course by default there's no $HOME variable defined. In that case bash looks in the root directory (the first four entries in the above list). But if you do set a $HOME, bash will look there (and not in the root dir). Smile

To make your life easy, you could place your shell/environment settings in /etc/profile (aka. /system/etc/profile). And while you're at it, don't forget to set $HOME and $USER too. Both might come handy.

P.S.: to automate the full process, you could replace the /system/bin/sh symlink with a simple shell script.
Eg.
#!/system/bin/bash
exec /system/bin/bash -l
Or ...
#!/system/bin/bash
[ -f /etc/profile ] && . /etc/profile
exec /system/bin/bash

P.S.2: the problem with bash was discussed quite extensively in a topic at xda-developers.com (although nobody got to the results that I've explained above), but unfortunately newly registered users are not allowed to comment in development related forums, so I couldn't share my findings there.

P.S.3: if for some reason your /system/bin/bash shell became setuid root (eg. RootLogger does that), then the bash started by ADB won't load /etc/profile because at this time the shell runs as the "shell" user and the config file is owned by root. However if right after ./adb shell you start a su -s /system/bin/bash -l root login shell, it'll load the config file's contents. The second variant of my /system/bin/sh replacement suggestions works around this problem too.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

How could i modify the source code of the shell in Android ICS?

Hi, is just that i've been trying to modify the source code of the sh shell of Android ICS, i am working with AOSP sources, but nothing happens when i modify the codes, it's like if it wouldn't load the changes or something like that, i have no idea why, i really need to make a few modifications, do you know if android has some flag or something that enables/disables this kind of modifications? (to the shell and shell commands)Y.

Best Regards.

Re: How could i modify the source code of the shell in Android

There could be a million+1 things wrong. I never had any problem with modding + compiling AOSP sources, but I never touched the shell's sources either. Are you familiar with Git? Did you properly check out a given revision or tag? Apart from these there're tons of basic stuff that could be wrong. Eg. double check that you've the correct source and that you're compiling the source files that you've modded. Sorry for not being too helpful, but your problem is quite generic. If nothing helps, you might try to strace the build process and see which files are actually opened/read, etc.