2

I'm using Ubuntu 11.10 and I have a strange problem.

I have a few exports in my .profile file:
export ANDROID_SDK_ROOT=~/workspace/android-sdk-linux_x86
export ANDROID_NDK_ROOT=~/workspace/android-ndk-r7
etc...

However, when I log on into GUI, open up my terminal and do echo $ANDROID_SDK_ROOT, it shows ~/workspace/android-sdk-linux_x86 rather than /home/brian/workspace/android-sdk-linux_x86.

Since it shows ~/workspace/android-sdk-linux_x86, it seems like .profile is read upon logging into GUI, but I'm not sure why tilde is not expanded.

If I run ". ~/.profile" in the terminal and check echo $ANDROID_SDK_ROOT, it shows /home/brian/workspace/android-sdk-linux_x86 fine. It's quite strange and I'm not sure why.

If I log into CUI (command user interface), when I do echo $ANDROID_SDK_ROOT, it prints /home/brian/workspace/android-sdk-linux_x86 as expected. So the problem only happens when I log into GUI.

Does anybody have any clue what's going on? I don't have .bash_profile in my home directory (I read somewhere that if I have .bash_profile, .profile is not read so I made sure .bash_profile does not exist; and I'm pretty sure .profile is read because it printed ~/workspace/android-sdk-linux_x86 when I did echo $ANDROID_SDK_ROOT). If you know what I might have done wrong, please shed some light.

Thanks!

Brian Park
  • 2,557
  • 22
  • 21

2 Answers2

2

In some cases (depending on the shell), ~ may be expanded only at the beginning of a word. You can work around it by using $HOME instead:

export ANDROID_SDK_ROOT=$HOME/workspace/android-sdk-linux_x86
export ANDROID_NDK_ROOT=$HOME/workspace/android-ndk-r7

(Note that this won't work for the ~username syntax, just for ~ expanding to your own home directory.)

In particular, bash does expand ~ in this context, but dash doesn't. On Ubuntu, /bin/sh is a symbolic link to dash; /bin/sh is the shell used by default for a lot of non-interactive activities.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Using tilde expansion with export is shell dependent (since some shells do not qualify assignment in export command as shell variable assignment). Thus, it's better to rewrite it as:

ANDROID_SDK_ROOT=~/workspace/android-sdk-linux_x86
export ANDROID_SDK_ROOT
ANDROID_NDK_ROOT=~/workspace/android-ndk-r7
export ANDROID_NDK_ROOT
pmod
  • 10,450
  • 1
  • 37
  • 50
  • What shells don't expand `~` in that context? All the ones I've tried do so (except for one version of `sh` that doesn't support `export FOO=BAR`). In any case, replacing `~` by `$HOME` should also work. – Keith Thompson Dec 09 '11 at 08:46
  • @Keith Thompson I heard that at least dash doesn't do that – pmod Dec 09 '11 at 09:15
  • You're right about dash, not sure why I missed that. And on Ubuntu, `/bin/sh` is a symlink to `dash` (`/bin/sh` is the shell that's invoked by default for a lot of non-interactive stuff). – Keith Thompson Dec 09 '11 at 09:23