Differences between revisions 1 and 2
Revision 1 as of 2007-10-11 17:23:45
Size: 991
Editor: client-86-31-53-220
Comment:
Revision 2 as of 2007-10-11 21:02:20
Size: 1511
Editor: GreyCat
Comment:
Deletions are marked like this. Additions are marked like this.
Line 24: Line 24:

Option #6 would be to write your function such that it 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 [:BashFAQ#faq80:FAQ #80] for more discussion of using functions instead of aliases.

Anchor(faq86)

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.

#5 is different from the others in that ignores aliases and functions. It has a few options which you might want to use, see help command.

Option #6 would be to write your function such that it 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 [:BashFAQ#faq80:FAQ #80] for more discussion of using functions instead of aliases.

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