I need to run a certain command when a push is received in a repository. That binary is located at a custom path which I added to .bashrc and works normally from the shell. I wrote a little script as a git hook. However, the .bashrc additions don’t seem to affect this script.
I’ve tried putting bash ~/.bashrc at the top of the script but didn’t work. If I run the script manually, say $ sh post-receive, it works just fine. What is wrong?
The script is probably run as a different user, or is being invoked with options to disable interactive features and/or startup scripts. That’s as it should be; nothing is “wrong” here really.
You can add
. /home/you/.bashrcas a quick and dirty workaround; or, more properly, just amend the script’s PATH directly; or, even more properly, modularize dependencies e.g. by putting the code in a separate file which you source both from your.bashrcand from this script – but for this isolated case, that´s certainly overkill.The command to “include” a file of shell scipt is called “source” or “dot”; in Bash,
sourceis available as a synonym, but in proper Bourne shell, it’s a literal dot (aka period, full stop):