How to get a bash instance with loaded default profiles within objective-C
Problem: I want to be able to run a bash instance from my cocoa application (OS X) with all the normal profiles loaded (~/.bash_profile, etc). I don't want to load the profiles manually since I want to 开发者_开发知识库have a default bash instance that is exactly the same as one you would get by firing terminal. From there, I'd like to retrieve some pre-defined environment variable (Ruby version manager's variables).
What I've tried: I've already tried some solutions with no success. Let me list them here:
- NSTask
- system() call
for every solutions I tried to execute "/bin/sh -l" to have a bash instance loaded as the current username... unfortunately it didn't work.
When you run bash
as sh
, it runs in a compatibility mode where it doesn't read .bash_profile
. If you want to run bash
then run /bin/bash
(or if you want other people to use your application, make sure you account for whatever shell the user has selected.)
You can use the command line option '--login
' to tell bash
to behave as a login shell.
Classically, a shell would act as a login shell if the basename of its argv[0]
started with a dash.
You might be able to get the required effect with:
bash --login -c 'echo $RUBY_VARIABLE_OF_INTEREST'
If you are doing this with popen()
, you can read the output from the shell.
精彩评论