Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script with Git bash with custom bashrc?

I am trying to get a bash script to run in git bash while specifying a different .bashrc than the one in my home directory (or none at all) however it is proving an impossible task.

To my understanding this should work:

"C:\Program Files (x86)\Git\bin\sh.exe " --rcfile .bashrc --login -i C:/Scripts/myscript.sh

However no matter what I try either the --rcfile file flag will be completely ignored or the script will get parse errors because it is not parsed by bash.

The following are my findings:

  • --login flag is needed to get the script to be parsed by bash rather than windows command prompt
  • --rcfile and also --norc are completely ignored if flags --login is used

I have tried every possible combination I think of, including calling the script within my .bashrc file, swapping the flags around, using the -c flag to run the script command and swapping my .bashrc files around to try using the --norc flag instead.

Is this just a result of shitty bash implementation for windows or am I doing something wrong?

Any help on the matter is appreciated.

like image 911
Alexandre Thenorio Avatar asked Oct 27 '25 07:10

Alexandre Thenorio


2 Answers

You can try sourcing your .bashrc inside the script myscript.sh.

source .bashrc

Or

. .bashrc
like image 98
Amit Verma Avatar answered Oct 28 '25 21:10

Amit Verma


As far as I can tell, the -i flag is overridden by the fact that you provide a script for bash to run. Your shell isn't actually interactive, so --rcfile is ignored. The only way I can tell to both run a script and source an additional file is to use a non-interactive login shell; however, in that case, you are restricted to using .bash_profile, .bash_login, or .profile, whichever is found first:

bash --login myscript.sh

There is no --loginfile to override the choice of file sourced prior to myscript.sh.


UPDATE: I forgot about BASH_ENV.

export BASH_ENV=.bashrc
bash myscript.sh

I do not know how you would go about adding BASH_ENV to your environment in Git bash.

like image 44
chepner Avatar answered Oct 28 '25 21:10

chepner