Search code examples
bashshellzsh

bash/zsh does not execute "dmesg | lolcat" properly


I want to write an alias for every command possible and want to concat "| lolcat" to it and then execute it.

So far I have:

     looool () {

      command=""
      for var in "$@"
        do
            command="$command $var"
        done

      command="$command | lolcat"

      $command
    }

and I test it with adding

looool dmesg

as last line.

The Command as string is dmesg | lolcat, but the output so far is the help page of dmesg and not the desired colored dmesg.

Thanks in Advance.


Solution

  • The two main problems are that:

    1. Building up the command into one string like that will break the arguments in a lot of cases (e.g. if one of them contains a space, or a wildcard)
    2. When you execute "$command", it won't interpret the pipe as a separator, because the shell splits the line into commands before it expands variables, so it's too late for the pipe to take effect. You could get round this with "eval", but the problem above means that's not really a good idea.

    Why don't you just do

    looool () {
        "$@" | lolcat
    }
    

    ?