1

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 1

3

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).

5
  • What does shift exactly do?
    – syntagma
    Commented Mar 30, 2015 at 17:54
  • I just pushed an edit to answer this.
    – dhag
    Commented Mar 30, 2015 at 17:59
  • This works for tail but not ls! I'm using function ls() { echo "Test" } (with newlines), but ls works as normal. Whereas function tail() { echo "Test" } works.
    – tog22
    Commented Sep 15, 2020 at 0:15
  • 1
    @tog22: My guess would be that you have an ls alias shadowing your ls function. Try type -a ls to find out, and possibly use unalias to get rid of the alias if it is not wanted.
    – dhag
    Commented Sep 15, 2020 at 14:24
  • Thanks, you got it!
    – tog22
    Commented Sep 15, 2020 at 19:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .