Reputation: 9047
Is there a way to make git source my .bashaliases
file by default when running the git submodule foreach
command?
For example, I've aliased git --no-pager grep -n
to ggrep
, and I frequently want to search all submodules with git submodule foreach "ggrep <PATTERN>; true"
, but the command simply prints "ggrep: not found" for each submodule.
Upvotes: 1
Views: 381
Reputation: 295291
Aliases aren't intended for noninteractive use, and even if they were sourced into the shell being used in question, they still wouldn't be available in this context without being explicitly turned on for that shell with shopt -s expand_aliases
.
If you really want to do this with an alias, though, you can do that. In your ~/.bash_profile
, put something like the following:
export BASH_ENV=$HOME/.env ENV=$HOME/.env
...and, in ~/.env
:
# attempt to enable expand_aliases only if current shell provides shopt
if command -v shopt; then
shopt -s expand_aliases
fi
alias ggrep='git --no-pager grep -n'
If your /bin/sh
is provided by bash, consider an exported function -- placing the following in your ~/.bash_profile
, as an example:
ggrep() { git --no-pager grep -n "$@"; }
export -f ggrep
(Unlike ~/.bashrc
, ~/.bash_profile
is executed only on login shells; however, since this command exports contents into the environment, shells invoked as child processes will inherit such contents).
If you don't have that guarantee, put a script in your PATH:
#!/bin/sh
exec git --no-pager grep -n "$@"
Note that the /bin/sh
shebang is used here as it's liable to be a smaller, lighter shell than bash, and exec
is used to avoid an extra fork()
to run the command as a subprocess.
Upvotes: 1