Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$PATH extended at .bashrc not available within git hook script

Tags:

git

bash

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?

like image 701
Toni Cárdenas Avatar asked Dec 21 '22 01:12

Toni Cárdenas


2 Answers

Check your .bashrc In some distrib, the end of the script is skipped when not exec in interactive mode. Just bring the usefull stuff you need over the skip part.

It looks to that:

# If not running interactively, don't do anything
case $- in
  *i*) ;;
  *) return;;
esac
like image 100
Clem Avatar answered Mar 03 '23 18:03

Clem


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/.bashrc as 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 .bashrc and 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, source is available as a synonym, but in proper Bourne shell, it's a literal dot (aka period, full stop):

. /path/to/stuff
like image 29
tripleee Avatar answered Mar 03 '23 17:03

tripleee