문제

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:

  1. NSTask
  2. 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top