Namely: I want to alias tail -f
to less +F
but let tail with any other parameter supplied work the same way as before.
1 Answer
This is slightly beyond the powers of what shell aliases provide (assuming bash). You could define a function:
function tail() {
if [ "$1" == '-f' ]; then
shift
less +F "$@"
else
command tail "$@"
fi
}
When you type tail
, this will now refer to the function defined
above, which checks its first argument, if any, for equality with
-f
, and if it matches, runs less +F
on the rest of the original
arguments (shift
removes the first of the original arguments,
-f
). Otherwise, it calls the command tail
with all of the original
arguments (calling the built-in command
is necessary to avoid an
infinite loop; without it, tail
would refer to the function being
defined, causing an infinite loop).
-
-
-
This works for
tail
but notls
! I'm usingfunction ls() { echo "Test" }
(with newlines), but ls works as normal. Whereasfunction tail() { echo "Test" }
works.– tog22Commented Sep 15, 2020 at 0:15 -
1@tog22: My guess would be that you have an
ls
alias shadowing yourls
function. Trytype -a ls
to find out, and possibly useunalias
to get rid of the alias if it is not wanted.– dhagCommented Sep 15, 2020 at 14:24 -