Search code examples
fish

Fish how to prevent recursive function calls


How can I prevent recursive function calls on fish functions when overriding a default binary with a function that has the same name ?

eg.

# Override 'ls'
function ls
    if [ my_special_condition ]
        * Do special stuff *
    else  # Call regular ls
        ls $argv
    end    
end

Obsiously the above code ends up in a resursive loop without calling the actual 'ls' binary. Is there a way to fix this ?


Solution

  • I understand that you want to replace the ls function while also being able to call the original. You can do that by copying the function via functions -c:

    functions -c ls orig_ls # copies ls to orig_ls
    function ls
        if [ my_special_condition ]
            * Do special stuff *
        else  # Call original ls
            orig_ls $argv
        end    
    end