Trying to run a function defined in my .bashrc using "bash -c ". I end up with the error "command not found". How do I get "bash -c" to load my init file?

2 Answers 2


You can make it into an interactive shell with -i, then your ~/.bashrc will be read:

bash -i -c "echo \$EDITOR"

The other thing you can do is source the file explicit. If you have /var/tmp/test with content:

export XXX=123

and you do

bash -c "source /var/tmp/test; echo \$XXX"

you will get 123 echoed.

  • This doesn't work either: the -i fails with this same issue ( stackoverflow.com/a/27581210/293735 ), and I cannot use the -l since that'd reload the .profile (which I want to avoid) and the explicit source doesn't work (bash 4.3.30 on Ubuntu, maybe it's platform specific?)
    – berdario
    Feb 24, 2015 at 23:14
  • @berdario the -i flag does work, it will run an interactive shell which reads ~/.bahshrc and, since it is interactive, also enables aliases. How exactly did it fail for you? You're linking to an answer on SO that does not describe failure and the question is very specific and is about calling bash from within a perl script. Is that what you're doing? In any case, I couldn't repeat the OP's issue, it worked as expected even within Perl.
    – terdon
    Feb 26, 2015 at 13:08
  • @terdon invoking bash -i stops the outer process, this happens with anything as the caller process (I personally tried with Python and Ruby)... but indeed something weird is happening: I could reproduce it on Ubuntu (I'll try again this evening at home), but I cannot reproduce it on Nixos and Kali
    – berdario
    Feb 27, 2015 at 13:58
  • @berdario it sounds like your issue is quite specific. I suggest you ask a new question and link back to this one explaining how the answers here don't work for you.
    – terdon
    Feb 27, 2015 at 13:59
  • Well, I think that this answer is wrong (and yours might be correct, but I want to double check on my machine before jumping to any conclusion).
    – berdario
    Feb 27, 2015 at 14:01

Another option would be to set the $BASH_ENV variable:

   When bash is started non-interactively, to  run  a  shell  script,  for
   example, it looks for the variable BASH_ENV in the environment, expands
   its value if it appears there, and uses the expanded value as the  name
   of  a  file to read and execute.  Bash behaves as if the following com‐
   mand were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to search for  the  file

So, you could do:

BASH_ENV=~/.bashrc && bash -c 'your_function'
  • bash --rcfile ~/.bashrc -c 'echo $FOO' doesn't print bar as expected
    – berdario
    Feb 24, 2015 at 23:05
  • @berdario no, it doesn't. Thanks for pointing it out, I could have sworn I had tested it. The problem is that the --rcfile is only used when launching an interactive shell and bash -c launches a non-interactive one. Setting BASH_ENV does work though, did you try that?
    – terdon
    Feb 26, 2015 at 12:59
  • This doesn't work either: with export FOO=bar in the .bashrc, > echo $BASH_ENV\n /home/dario/.bashrc\n > bash -c 'echo $FOO'\n: no output except the newline
    – berdario
    Feb 27, 2015 at 22:46
  • 1
    @berdario I'm not sure exactly what you did but try this: BASH_ENV=~/.bashrc bash -c 'echo $FOO'. I just tested that on my system with FOO=bar in ~/.bashrc and it worked as expected. If it doesn't, please ask a new question and link back to this one because your issue will be different.
    – terdon
    Feb 27, 2015 at 23:06
  • Upon running set -xe && source ~/.bashrc I realized that apparently bash can silently swallow some errors when sourcing... these errors will make the sourcing silently fail. Since this got out of hand I opened a new question: unix.stackexchange.com/questions/187738/…
    – berdario
    Mar 2, 2015 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy