Differences between revisions 6 and 7
Revision 6 as of 2009-05-29 15:53:44
Size: 1565
Editor: localhost
Comment:
Revision 7 as of 2009-08-30 21:09:35
Size: 1632
Editor: localhost
Comment: fix a minor error about the command builtin and some clarifications
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
command grep     #5 command grep ... #5
Line 22: Line 22:
#3 and #4 are the same, allowing you to run `grep` once while ignoring the `grep` alias. #3 and #4 are the same, allowing you to run `grep` once while ignoring the `grep` alias, but not functions
Line 24: Line 24:
#5 is different from the others in that it ignores aliases, functions, '''and''' builtin bash commands (such as `time`, `echo`). It has a few options which you might want to use -- see `help command`. #5 is different from the others in that it ignores aliases, functions, and shell keywords such as `time`. It will still prefer shell builtins like `echo` rather than /bin/echo. It has a few options which you might want to use -- see `help command`.

How to ignore aliases or functions when running a command?

Sometimes it's useful to ignore aliases (and functions, including shell built-in functions). For example, on your system you might have this set:

alias grep='grep --color=auto'

But sometimes, you need to do a one-liner with pipes where the colors mess things up. You could use any of the following:

unalias grep; grep ...    #1
unalias -a; grep ...      #2
"grep" ...                #3
\grep ...                 #4
command grep ...          #5

#1 unaliases grep before using it, doing nothing if grep wasn't aliased. However, the alias is then gone for the rest of that shell session.

#2 is similar, but removing all aliases.

#3 and #4 are the same, allowing you to run grep once while ignoring the grep alias, but not functions

#5 is different from the others in that it ignores aliases, functions, and shell keywords such as time. It will still prefer shell builtins like echo rather than /bin/echo. It has a few options which you might want to use -- see help command.

Option #6 would be to write your function which does not commit undesirable behavior when standard output is not a terminal. Thus:

ls() {
  if test -t 1; then
    command ls -FC "$@"
  else
    command ls "$@"
  fi
}

Using this instead of alias ls='ls -FC' will turn off the special flags when the function is being used in a pipeline (or any other case where stdout isn't a terminal).

See FAQ #80 for more discussion of using functions instead of aliases.

BashFAQ/086 (last edited 2022-06-11 16:37:18 by ormaaj)