0

I want to create an alias, that can be defined on any command and concats | lolcat to it. So far I googled myself to

alias MyCommandName='f(){"$@" | lolcat; unset -f f};f'

but when I test it with dmesg I get no result.

# set dmesg
alias dmesg='f(){"$@" | lolcat; unset -f f};f'
#desired command: dmesg | lolcat
dmesg

In my understanding the alias defines a function f, that is called afterwards. To prevent recursion, the last statement is to unset the function.

EDIT: I stopped at:

    alias MyCommandName='f(){eval "$@ | lolcat"; unset -f f};f' 
    alias MyCommandName='f(){eval "$0 $@ | lolcat"; unset -f f};f'

which does not the trick, because i can't pass the $0 argument into my function. The $0 is always f inside of the function. a generic aliaslias like is NOT possible. :(

NOT A DUPLICATE of this, because I am using ZSH and argument passing is possible according to like 10 other answers like this and this :).

KuSpa
  • 125

1 Answers1

2

If I understand right, you want to do something that takes a command name, say dmesg, and make it into something pipes its output to lolcat.

This is still best done using functions:

dmesg () { command "$0" "$@" | lolcat; }

That's as "generic" as it comes. The command command skips functions and calls builtins or executables: What is use of command: `command`?. $0, as you realised, is the name of the function, in this being the name of the command as well. So command "$0" runs the command with the same name as the function.

muru
  • 207,228