6

I have the following alias:

alias mv='mv -i'
alias git='LANG=en_US \git '

then when I do a git mv command the mv is interpreted as mv -i:

$ git mv a b
error: unknown switch `i'

I would like the alias to apply only if it is a bash command

Versions:

  • Ubuntu 16.04.3 LTS
  • GNU bash, version 4.3.48(1)
  • git version 2.7.4 (also I don't think it's linked to git)

Notes:

  • git \mv a b works
  • unaliasing git also works \git mv a b
  • 2
    I can't really reproduce this with Git 2.12.4 and Bash 4.4.12. – Kusalananda Aug 31 '17 at 7:56
  • 4
    In bash v4.4, aliases only work if the alias is the first word on a line. Demo: alias woo="woo hoo" ; echo woo outputs woo. – agc Aug 31 '17 at 8:00
  • I would call this alias mvi or something similar. – pfnuesel Aug 31 '17 at 8:01
  • Are you sure? A workaround could be to make mv a bash function (not an alias). – Basile Starynkevitch Aug 31 '17 at 8:08
  • 1
    I've got the same versions installed here, and I can't reproduce that either. Must be something special with your bash configuration; you could check the aliases and environment for something git related. What do you get when trying type -a mv and type -a git? – Murphy Aug 31 '17 at 8:26
14

Since git is an alias ending with a space, bash performs alias expansion on the word immediately after it:

$ alias mv='mv -i'
$ alias git=': git '
$ set -x
$ git mv
+ : git mv -i

From the docs:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

Make git an alias without the space:

alias git='LANG=en_US git'

Note that:

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to ls -F, for instance, and Bash does not try to recursively expand the replacement text.

So, you don't need \git there.

  • You are right, removing this extra space did the job, thanks ! – jo_ Aug 31 '17 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.