Bash Alias Problem

aliasbashgrep

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?

Best Answer

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'
Related Question