3

I can't understand why using alias prompts an error, but using the same syntax without alias does not...

alias grep='egrep -iIs '
19:47:24 ~
cat /etc/services | \grep ssh
ssh     22/tcp              # SSH Remote Login Protocol
19:47:26 ~
cat /etc/services | grep ssh
grep: option requires an argument -- 'X'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
19:47:33 ~
cat /etc/services | egrep -iIs ssh
ssh     22/tcp              # SSH Remote Login Protocol

Can you explain this?

  • Cannot reproduce it - works fine here on Fedora24. – NickD Sep 14 '17 at 18:02
  • Works fine for me with GNU grep but note that -I is not POSIX. What OS are you using? – John1024 Sep 14 '17 at 18:02
  • 3
    Do you by any chance also have ssh aliased to ssh -X? if so, I suspect the trailing space in your alias grep='egrep -iIs ' is allowing that to be expanded to ssh -X - resulting in the additional -X argument being passed to grep – steeldriver Sep 14 '17 at 20:16
  • 1
    @steeldriver That's the only possible reason, you should make this an answer. – xhienne Sep 14 '17 at 23:42
  • 2
    I bet I'm not the only one to wonder what may be that unknown -X option to grep. So here is the answer... along with further questions. – xhienne Sep 15 '17 at 0:33
10

As noted in the ALIASES section of man bash

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.

Since you have defined your grep alias with such a blank last character, the shell will also expand any alias that you have defined for ssh - if that is alias ssh='ssh -X' for example, then the command will become

cat /etc/services | egrep -iIs ssh -X

which passes the -X as an additional argument to egrep.

Unless there is a particular reason to include the trailing blank, simply define the alias without it, i.e.

alias grep='egrep -iIs'

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.