What I want to achieve is to change the default behavior of a command + argument
, specifically tail -f
. This command shows, by default, the last 10 lines of the file and follows its changes. I want it to show 50 lines, instead of 10. The corresponding command for that is tail -fn 50
so I thought about creating an alias with something like this:
alias 'tail -f'='tail -fn 50'
But it doesn’t work.
Is there a way to create an alias with a command + argument ‘name’ to substitute the original command?
How can I achieve this, if not?
4
It’s very difficult to do that sort of thing with an alias
, but it’s trivial with a function. That’s good, because you should have replaced all your aliases with functions 20 years ago! To get the behavior you’re asking for, you could do:
tail() { case $1 in -f) shift; set -- -fn 50 "$@"; esac; command tail "$@"; }
2
If you try your approach, you should get an error message
bash: alias: `tail -f': invalid alias name
at least if you have a not-too-old bash version (If I remember correctly, very old bash allowed you to define such an alias, but you couldn’t call it). The reason is that a space is not a valid character in an alias name.
Hence you first have to think about a valid name for the alias:
alias tail-f='tail -fn 50'
Now you can execute
tail-f FILENAME
while a
tail -f FILENAME
would still exhibit the original behaviour of tail
.