I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).
Context:
- If I open Git Bash from windows menu, I can see the alias working (
php -v
returns correct info) - In my PHP project, I set a pre-commit hook to run some tests using
php
command
I added this aliases in both .bashrc
and .bash_profile
files:
export PATH=$PATH:"/c/Users/MyUser/.config/herd/bin/"
# Laravel Herd Aliases - please do not remove these lines
alias php="php.bat"
alias herd="herd.bat"
alias laravel="laravel.bat"
alias composer="composer.bat"
Now, when I execute git commit something
, I get this error:
which: no php in (/mingw64/libexec/git-core:/mingw64/libexec/git-core:/mingw64/bin:/usr/bin: ...etc other paths)
/c/ProgramData/ComposerSetup/bin/composer: line 14: php: command not found
What I do not understand is that the alias seems to work If I use in git bash, but it's not working in normal git flow.
I am not able to set a PHP alias in the git bash installed in windows (I'm using Herd).
Context:
- If I open Git Bash from windows menu, I can see the alias working (
php -v
returns correct info) - In my PHP project, I set a pre-commit hook to run some tests using
php
command
I added this aliases in both .bashrc
and .bash_profile
files:
export PATH=$PATH:"/c/Users/MyUser/.config/herd/bin/"
# Laravel Herd Aliases - please do not remove these lines
alias php="php.bat"
alias herd="herd.bat"
alias laravel="laravel.bat"
alias composer="composer.bat"
Now, when I execute git commit something
, I get this error:
which: no php in (/mingw64/libexec/git-core:/mingw64/libexec/git-core:/mingw64/bin:/usr/bin: ...etc other paths)
/c/ProgramData/ComposerSetup/bin/composer: line 14: php: command not found
What I do not understand is that the alias seems to work If I use in git bash, but it's not working in normal git flow.
Share Improve this question edited Mar 24 at 7:32 Giacomo M asked Mar 24 at 7:23 Giacomo MGiacomo M 4,7258 gold badges31 silver badges68 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 3There's a whole list of reasons:
Aliases are internal to the shell they were defined in; they don't automatically propagate "down" into programs (even if those programs are additional instances of Bash).
Bash only loads
~/.bashrc
when running in "interactive" mode. If run in non-interactive mode (like for a .sh script or asystem()
command), it doesn't load any startup scripts at all, and therefore won't load your alias definitions.So although your interactive shell has the aliases, any additional shell process run by
git
(like a hook script) won't have them.Bash only expands aliases when running in "interactive" mode. If run in non-interactive mode (like for a .sh script or a "system()" command), it ignores all alias definitions it may have – though per #1, in this mode it won't have any alias definitions in the first place.
Not all command invocations even go through Bash in the first place. Yes, things like "system()" go through the shell, but the parent program can just as well execute another program directly (using the equivalent of PHP's "pcntl_fork()" + "pcntl_exec()".)
Related to #3: As your message shows, the error occurs not when trying to execute the
php
command, but when trying to find it. That is, the failing script tries to do an early check usingwhich
to make sure that such a command exists in $PATH – and aliases do not exist in $PATH, so the script's pre-check cannot succeed. (That is, assuming if the script had any alias definitions, which it doesn't per #1.)- This is in part because the check is done using the external
which
instead of the shell built-incommand -v
. Aliases are completely internal to the shell they were defined in, so even if the shell in question had any alias definitions (which per #1 it doesn't), those wouldn't be visible to thewhich
program as it is external to the shell.
- This is in part because the check is done using the external
Recommendation: Replace the alias with a "real" shell script, e.g. ~/bin/php
. Then you will only need to ensure that ~/bin is in PATH, and it's much easier to have PATH propagate down into programs (it's automatic) than to make the same happen with aliases.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744256247a4565412.html
git
, not only thealias
definition is present, but also ashopt expand_aliases
is done. Otherwise bash would ignore the alias. I don't know which file you have to place this settings into. Perhaps you have to create a separate file for this and do anexport BASH_ENV=/abs/path/to/this/file
before calling git. See the section called INVOCATION in the bash man page. – user1934428 Commented Mar 24 at 14:24shopt
, does it show the stringexpand_aliases
in the output? If it does, your bash should be able to get it set in a script as well. But in your case, I think you didn't put them into the right files. If git itself executes bash code, it will likely do it using a bash child process, and I don't see why such a process should source .bash_profile or .bashrc. – user1934428 Commented Mar 25 at 10:48