Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I like to encapsulate commands within shell-functions using the same name. But to avoid the shell-function calling itself recursively, I specify the complete path of the command as the following example:

less()
{
    test 0 -lt $# && echo -ne "\e]2;$@\a\e]1;$@\a"     # Window title
    /usr/bin/less -i -rS --LONG-PROMPT --shift 5 "$@"
}

But for some commands, I do not want to specify the path because it may change. I would prefer to use $PATH.

For instance, my following attempt failed to call mvn command using backslash: \mvn

mvn()  # colorizes maven output
{
        \mvn "$@" 2>&1 | #here: the shell-function recursively calls itself indefinitely
        sed -u '                                             
    s/^\[ALL\].*/\o033[1;37m&\o033[0m/
  s/^\[FATAL\].*/\o033[1;31m&\o033[0m/
  s/^\[ERROR\].*/\o033[1;31m&\o033[0m/
s/^\[WARNING\].*/\o033[1;33m&\o033[0m/
   s/^\[INFO\].*/\o033[1;37m&\o033[0m/
  s/^\[DEBUG\].*/\o033[1;36m&\o033[0m/
  s/^\[TRACE\].*/\o033[1;32m&\o033[0m/'
}

What is the best way to bypass this issue?
Please do not suggest to use a different shell-function name.

(I usually use bash but I am interested about other shell solutions.)

share|improve this question
2  
zsh answer, but applies for bash too: How can I create a function in zsh that calls an existing command with the same name?. –  manatwork Feb 26 '13 at 10:53
add comment

marked as duplicate by manatwork, rahmu, uther, Renan, jasonwryan Feb 26 '13 at 20:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 5 down vote accepted

Prepend your actual commands (not functions) with command shell builtin, it has exactly the purpose you're looking for. Therefore your shell-function should look that:

mvn()
{
        command mvn "$@" 2>&1 |
        sed -u '
    s/^\[ALL\].*/\o033[1;37m&\o033[0m/
  s/^\[FATAL\].*/\o033[1;31m&\o033[0m/
  s/^\[ERROR\].*/\o033[1;31m&\o033[0m/
s/^\[WARNING\].*/\o033[1;33m&\o033[0m/
   s/^\[INFO\].*/\o033[1;37m&\o033[0m/
  s/^\[DEBUG\].*/\o033[1;36m&\o033[0m/
  s/^\[TRACE\].*/\o033[1;32m&\o033[0m/'
}
share|improve this answer
add comment

It depends on what shell you use. By default zfs, bash and ksh does not expand aliases with the same name as the alias, in aliases. This is specifically to avoid [usually unintentional] loops.

Therefore you are safe to make commands like you want to do.

alias ls="echo Hello; ls"

Is perfectly fine.

share|improve this answer
1  
The question was about function, not alias. –  manatwork Feb 26 '13 at 11:00
    
Note that adding the full path to the command inside the alias sometimes gives a benefit in that that command will run without having to search the PATH!!! –  Johan Feb 26 '13 at 11:00
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.