Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running bash programmatically from objective c to create a terminal-like application. Once launched I would like to execute some commands prior to turning control over to the user. Normally these would be in the .bash_profile. This file is not accessible to me and not something I would want to mess we anyway as I don't want to effect the standard bash environment for the user. I tried the -c script.sh switch but after script.sh is done running, bash exits. Are there any other options I have other than programmatically typing out the command I need run? I would rather not have the user see everything.

share|improve this question

You could try the following:

bash --rcfile <your_rc_file> --noprofile -i

What the options mean:

  • --rcfile: Use the following as the startup file
  • --noprofile: Do not process the files .bash_profile, .profile, /etc/profile, etc. Files used normally in login shell.
  • -i: Its an interactive shell (kinda like opposite of -c which exits onces the argument to -c is processed).

The manpage of bash is quite good. You could use it for more details on how bash can be launched.

share|improve this answer

You can invoke bash with --rcfile myfile and have test -f ~/.bashrc && source ~/.bashrc at the end of myfile. This assumes you're launching an interactive but non-login shell.

For a login shell you would have to mimic bash's behavior with regard to /etc/profile, .profile, .bash_profile, .bash_login and .bash_logout, which is tedious but doable. You would still run bash --rcfile myfile but myfile would contain

test -r /etc/profile && source /etc/profile

for f in ~/.bash_profile ~/.bash_login ~/.profile
   if test -r $f
      source $f

trap 'source ~/.bash_logout' 0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.